Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash if中的Sed错误";sed:无输入文件“;什么时候试着换线_Bash_For Loop_Sed_While Loop_Sh - Fatal编程技术网

Bash if中的Sed错误";sed:无输入文件“;什么时候试着换线

Bash if中的Sed错误";sed:无输入文件“;什么时候试着换线,bash,for-loop,sed,while-loop,sh,Bash,For Loop,Sed,While Loop,Sh,我尝试更改yaml文件中的副本数。我的脚本需要我的信息,并且必须从键盘上读取文本,但他跳过了读取并向我发送“sed:无输入文件”。我做错了什么?谢谢大家的关注 #!/bin/bash FILES=./test1/* for file in $FILES do echo "$file" grep -v -e "commonconfigs" -e "commonsecrets" -e "internalurls" $file | grep -w "[^-] name:\|repli

我尝试更改yaml文件中的副本数。我的脚本需要我的信息,并且必须从键盘上读取文本,但他跳过了读取并向我发送“sed:无输入文件”。我做错了什么?谢谢大家的关注

#!/bin/bash
FILES=./test1/*

for file in $FILES
do
    echo "$file"
    grep -v -e "commonconfigs" -e "commonsecrets" -e "internalurls" $file | grep -w "[^-] name:\|replicas:"
    while IFS= read line
    do
    currentline=$(echo "$line" | grep -o "replicas")
      if  [ "$currentline" == "replicas" ];then
        read -p "replics:" rep
        sed -i 's/replicas:.*/replicas:$rep/g' > "$line"
      fi
    done < "$file"
done
示例文件

apiVersion: apps/v1
kind: Deployment
metadata:
  name: devices-api
spec:
  selector:
  replicas: 1
  template:
    metadata:
        fluentd: "true"
      annotations:
        prometheus.io/scrape: 'true'
    spec:
      containers:
      - name: devices-api
        image:
        imagePullPolicy: "Always"
        resources: {}
        env:
          - name: Swagger__Enabled
            valueFrom:
              configMapKeyRef:
                name: commonconfigs
---

apiVersion: apps/v1
kind: Deployment
metadata:
  selector:
      app: event-jobs
  replicas: 1
  template:

您的下一个代码声明是有罪的:

do [...] sed -i 's/replicas:.*/replicas:$rep/g' > "$line" [...] done < "$file"
do[…]sed-i的/replications:.*/replications:$rep/g'>“$line”[…]完成<“$file”

使用选项-i调用的sed命令用于在位编辑文件,如果从标准输入(do while循环之外)读取文件,则无法执行此操作

如果您的目标是替换yaml文件中的
副本:
,则无需在
grep
时使用
。使用此
sed
命令:

sed "s/\<replicas:.*/replicas: $rep/g" test1/*yaml

sed“s/\谢谢大家的回答。我的问题在于我的算法解析。我试着阅读每一行并在sed中设置它们

sed -i "$32 s|replicas:.*|replicas: $rep|" $file
现在我用grep找到了行数,并将它们设置在数组中,然后设置在sed中,在那个里我需要更改一些内容

解析我的文件的解决方案:

#!/bin/bash
FILES=*yaml

for file in $FILES
do
    echo $file
    grep -v -e "commonconfigs" $file | grep -w "[^-] name:\|replicas:"
    nummeric=($(grep -n "replicas" $file | cut -f1 -d:))
    for i in "${nummeric[@]}"; do
      echo $i
      read -p "replicas: " rep
      sed -i "$i s|replicas:.*|replicas: $rep|" $file
    done
done
#!/bin/bash
FILES=*yaml

for file in $FILES
do
    echo $file
    grep -v -e "commonconfigs" $file | grep -w "[^-] name:\|replicas:"
    nummeric=($(grep -n "replicas" $file | cut -f1 -d:))
    for i in "${nummeric[@]}"; do
      echo $i
      read -p "replicas: " rep
      sed -i "$i s|replicas:.*|replicas: $rep|" $file
    done
done