Search code examples
bashfor-loopsedwhile-loopsh

Sed error in if "sed: no input files" when try replace lines


I try change replicas count in yaml files . My script find need me information and must read text from keyboard but he skip read and send me "sed: no input files". What I do wrong ? Thank you for attention

#!/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

Output:

./test1/anal.yaml
  name: anal
  replicas: 1
  name: anal
template:
sed: no input files

Example file

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:

Solution

  • Next statement of your code is guilty:

    do [...] sed -i 's/replicas:.*/replicas:$rep/g' > "$line" [...] done < "$file"
    

    The sed command called with the option -i is used to edit the file in place, which is not possible if you read the file from the standard input (outside of the do-while loop).