0

I have s simple bash script as follow:

interval=""
cat conf.param|\
while read param
do
   item_=$(echo $param|cut -d "=" -f1)    
   case ${item_} in
        interval)
                interval=$(echo $param|cut -d "=" -f2)                
                echo $interval
                ;;
        method)
                method=$(echo $param|cut -d "=" -f2)
                ;;
  esac
done
echo "${interval}"

It reads the contents of a file and stores them in different variables. the issue is that the variables are not set properly inside case segment. I put two echos. The first one (inside case) displays the interval value correctly which is '2', but the second one just after the esac statement displays nothing! it shows an empty blank line. the conf.param is a simple text file. it has more lines I only printed two lines:

interval=2
method="POST"
Sinai
  • 620
  • 1
  • 14
  • 36
  • You should post the `conf.param` file as well, I can only assume that the value is written and displayed correctly, and then written again with a blank string so it displays again as a blank string, but I need your data file to verify that. – Moshe Gottlieb Sep 26 '19 at 07:49
  • As an aside, the `echo | cut` is brittle and inefficient, and you have [quoting errors.](/questions/10067266/when-to-wrap-quotes-around-a-shell-variable/27701642) The shell has built-in parameter substitution operators so `item=${param%%=*}` and `value=${param#$item=}` – tripleee Sep 26 '19 at 08:24

1 Answers1

1

Your problem is that using a pipe ("cat conf.param | while read param"), you call a second shell which can not export its variables to the calling one. See this example:

interval=""
cat tmp.txt | while read param; do       ##### DON'T DO THIS
  interval="A$interval"
done
echo "First attempt: $interval"

interval=""
while read param; do
  interval="A$interval"
done < tmp.txt                           ##### BUT DO THIS INSTEAD
echo "Redirection attempt: $interval"

The file tmp.txt contains 4 lines; the output of the script is:

First attempt:

Redirection attempt: AAAA

As you see, the first attempt retains the old value of interval. The second/redirection attempt instead works because no new process is created.

Community
  • 1
  • 1
  • 1
    Maybe also point out that [the `cat` is useless](/questions/11710552/useless-use-of-cat) – tripleee Sep 26 '19 at 08:21
  • @tripleee in the "pipe" form (the first), cat is not totally useless: I like it because the command is very clear. In the redirection form, the second, you must scroll to the end of the cycle in order to see what you are applying the cycle to. I hate this... – linuxfan says Reinstate Monica Sep 26 '19 at 08:25
  • You can put the redirect before the command, but usually when we point this out, the complaint changes to move the goal posts somewhere else. – tripleee Sep 26 '19 at 08:29
  • Oh thanks @linuxfan. You are right. I want to set variables inside a while loop so I have to use redirection. my issue is solved by your answer – Sinai Sep 26 '19 at 08:36
  • thank you @tripleee, that was a good point. I always use simple syntax in my codes. but now I use built-in parameter substitution operators as you mentioned. – Sinai Sep 26 '19 at 08:38