This question already has answers here:
Syntax error in shell script with process substitution
(4 answers)
Closed 3 years ago.
I wonder why it doesn't work.
Please advise me.
1. working
$ nu=`awk '/^Mem/ {printf($2*0.7);}' <(free -m)`
$ echo $nu
1291.5
2. not working
$ cat test.sh
#!/bin/bash
nu=`awk '/^Mem/ {printf($2*0.7);}' <(free -m)`
echo $nu
$ sh test.sh
test.sh: command substitution: line 2: syntax error near unexpected token `('
test.sh: command substitution: line 2: `awk '/^Mem/ {printf($2*0.7);}' <(free -m)'
Could you please try following.
nu=$(free -m | awk '/^Mem/ {print $2*0.7}')
echo "$nu"
Things taken care are:
Use of backtick is depreciated so use $ to store variable's value.
Also first run free command pass its standard output as standard input to awk command by using |(which should be ideal way of sending output of a command to awk in this scenario specially) and save its output to a variable named nu.
Now finally print variable nu by echo.
Since <(...) process substitution is supported by bash not by sh so I am trying to give a solution where it could support without process substitution (which I mentioned a bit earlier too).
The <( ) construct ("process substitution") is not available in all shells, or even in bash when it's invoked with the name "sh". When you run the script with sh test.sh, that overrides the shebang (which specifies bash), so that feature is not available. You need to either run the script explicitly with bash, or (better) just run it as ./test.sh and let the shebang line do its job.
The reason to add a shebang in a script is to define an interpreter directive if the file has execution permission.
Then, you should invoke it by, for example
$ ./test.sh
once you have set the permission
$ chmod +x test.sh
Related
This question already has answers here:
Why does my Bash code fail when I run it with 'sh'?
(2 answers)
Closed 10 months ago.
I am kind of new in shell scripting and trying to learn arrays. I declared array value but when I am trying to print that array it is giving me an error(bad substitution).
I am pasting the code below, please suggest to me what is wrong here-
➜ ~ cat test.sh
#!/bin/bash
array=['foo','bar','a','b']
echo 1
echo "${array[0]}"
➜ ~ sh test.sh
1
test.sh: 5: Bad substitution
Thanks in advance.
Depending on the system you're using sh might be not be Bash and
it's not Bash on yours, it can be dash for example. Run your script with Bash:
$ bash arr.sh
1
[foo,bar,a,b]
Or set an executable bit and call the script without providing the name of the interpreter since you already have the shebang:
$ chmod +x test.sh
$ ./test.sh
1
[foo,bar,a,b]
This question already has answers here:
Difference between sh and Bash
(11 answers)
Closed 1 year ago.
I am running a bash script in which one line is this:
VERSION=$(awk -F. '{print $2}' <<< $BISMARK)
VERSION=$(cut -d '.' -f2 <<< $BISMARK )
but getting the following error from this line (when I comment out this line I will not get any error).
Syntax error: redirection unexpected
do you know what the problem is?
It would seem you are not actually running the script with Bash, but with some other shell instead. Your code works fine for me on Bash, but executing it with BusyBox's ash for example results in the error you mentioned.
What is the first line of your script? It should be either:
#!/bin/bash
or:
#!/usr/bin/env bash
Also, how do you execute the script? If the first line is correct, you should run it like this:
./script.sh
or alternatively like this:
bash script.sh
I am trying to write shell script (sh), Where I am getting below error
variable i contains:
test.txt
code:
echo "${i/.txt/}"
Error:
just.sh: 16: just.sh: Bad substitution
expected output string :
text
Reproduce steps
Create file:
touch text.txt
Create file test.sh contents using any of editor
code:
#!/bin/sh
for i in `find *.txt`
do
echo "$i"
echo "${i/.txt/}"
done
How to run:
sh test.sh
sh is not bash. Fix your shebang (the 1st line) as #!/bin/bash first.
References
Difference between sh and bash, search "expansion" in the thread
Bash features a rich set of expanded non-standard parameter expansions such as ${substring:1:2}, ${variable/pattern/replacement}, case conversion, etc.
I am trying to understand why the script will work with #!/bin/bash but not #!/bin/sh. I am running Cygwin and both sh.exe and bash.exe seem to be identical (same file size).
$ cat 1.sh
#!/bin/sh
while read line; do
echo ${line:0:9}
done < <(help | head -5)
$ ./1.sh
./1.sh: line 4: syntax error near unexpected token `<'
./1.sh: line 4: `done < <(help | head -5)'
$ cat 2.sh
#!/bin/bash
while read line; do
echo ${line:0:9}
done < <(help | head -5)
$ ./2.sh
GNU bash,
These she
Type `hel
Use `info
Use `man
Despite being same file, shell analyzes its own name when run and switches to either plain shell or bash mode.
The Problems
Bash is a superset of the Bourne shell, so many things are possible in Bash that aren't possible in more limited shells.
Even when sh is a hardlink to bash, it behaves differently when invokes as sh. Many features supported by the Bash shell will not work in this mode.
The Solutions
Remove your bashisms from the script, including nifty features like the process substitution you have on line 4.
Run your script as Bash, not vanilla Bourne.
I have the following question:
When I execute the following script directly in a terminal window, the commands behave as expected.
$ diff <(echo tmp) <(echo tmp1)
1c1
< tmp
---
> tmp1
However when I write the same command in a shell script
#! /bin/bash
diff <(echo tmp) <(echo tmp1)
I get the following error message:
$ sh test.sh
test.sh: line 2: syntax error near unexpected token `('
test.sh: line 2: ` diff <(echo tmp) <(echo tmp1)'
Initially I thought this was an issue with diff, but this also happens with other commands. Does anybody have an idea what causes the problem?
Try
bash test.sh
or
chmod ugo+x test.sh
./test.sh
Works fine for me when I do either.
Looks like the syntax is not supported by the bourne shell (sh).
When bash is invoked using sh, it starts up in a special, POSIX-compliant mode. This has different syntax, which I guess explains the different results.
See bashref of POSIX mode, #22: "process substitution is not available".
That syntax doesn't look familiar. Are you sure you are using bash in your terminal? You can verify by typing echo $SHELL.