0

Here's my problem:

I have 4 scripts to generate diverse files. I have a source file i pass as an argument to generate the other output files. The 4 scripts are thought to be run consecutively. They have code like this:

while read line; do                                                             
    if echo "${line: -3}" | grep -q ','                                         
    then                                                                        
        if echo "${line:0:4}" | grep -q 'int'                                   
            then ...

Running them separately give me the right outputs. Calling them from a main script performs wrong, with some error messages like:

./FirstScript.sh: 35: ./FirstScript.sh: Bad substitution

Referring i.e. to the line above:

if echo "${line: -3}" | grep -q ','

My main Script to call the others is:

#!/bin/bash                                                            
sh ./FirstScript.sh $1
sh ./SecondScript.sh
sh ./ThirdScript.sh $1

All Scripts have been set to $chmod 755 *.sh

Joster
  • 359
  • 1
  • 4
  • 19

3 Answers3

2

What is sh on your system? Your #! line looks like you want the scripts to be interpreted with bash, but your main script is using sh , which may not be as feature-complete as bash.

eduffy
  • 39,140
  • 13
  • 95
  • 92
  • 2
    It is by definition not as feature-complete as `bash`. Running `bash` as `sh` will disable many `bash` extensions. – chepner Jul 11 '16 at 13:44
  • That is one thing I still can't differ very well. Is Bash some kind of 'evolved sh script'? In that case how can I call them as Bash Scripts. An why running them separately as 'sh ./xxxx.sh' works fine, but not calling them from another script – Joster Jul 11 '16 at 13:46
  • That was certainly the problem. – Joster Jul 11 '16 at 13:59
2

You are calling them as sh even though your shebang points to bash. If you want to call them as bash just replace sh with bash , eg

#!/bin/bash                                                            
bash ./FirstScript.sh $1
bash ./SecondScript.sh
bash ./ThirdScript.sh $1

Sh lacks some of the features bash holds, which might be causing errors. See Difference between sh and bash for more information about this.

Community
  • 1
  • 1
Zuzzuc
  • 148
  • 9
0

In fact, as I stated #!/bin/bash in the first sentence, I can do:

#!/bin/bash                                                            
./FirstScript.sh $1
./SecondScript.sh
./ThirdScript.sh $1

As it is said to run every executable with /bin/bash.

Joster
  • 359
  • 1
  • 4
  • 19