Command errors when run with #!/bin/sh [duplicate] - bash

This question already has answers here:
Doesn't sh support process substitution <(...)?
(3 answers)
Closed 6 years ago.
Command inside a shell script:
mapfile -t my_array < <( grep '$(ABC)' ${file} |awk -F_ '{print $2}')
Fails when run with #!/bin/sh.
error: syntax error near unexpected token `<'
The same command works with #!/bin/bash.

Process substitution is not a feature present in POSIX sh; it is a ksh extension also adopted by bash and zsh.
Using #!/bin/sh only guarantees presence of functionality given in the POSIX sh specification.

Related

how to fix redirection unexpected syntax error [duplicate]

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

How can I create a filename using bash command parameter? [duplicate]

This question already has answers here:
How to use > in an xargs command?
(4 answers)
Closed 2 years ago.
I'm trying to create a filename using command param as such but not sure how to go about doing it. This is what I was trying:
echo "AZ" |xargs date >> $1.txt
I'm trying to create a file named AZ.txt with the date in it.
If you must use xargs, then you'll have to wrap the rest of it in a shell script to delay execution of the redirection until you have constructed the filename:
echo AZ | xargs sh -c 'date >> "$1".txt' sh
The trailing "sh" is to force the xargs argument "AZ" into $1 instead of $0
If the name of the file comes from some command, you can use command substitution:
date > "$(echo AZ)".txt

Why is using the ”./“ and ”bash“ to run the script result is different? [duplicate]

This question already has answers here:
Why is #!/usr/bin/env bash superior to #!/bin/bash?
(8 answers)
Associative arrays: error "declare: -A: invalid option"
(10 answers)
Closed 3 years ago.
I want to use curl command to get strings at cloud and parse them into
a dictionary.
my shell code :
#!/bin/bash
#
URL=https://raw.githubusercontent.com/Nova-He/python/master/base_images
declare -A dic
for x in $(curl -s $URL);do
dic+=([$(echo $x |cut -d/ -f1)]="$(echo $x |cut -d/ -f2)")
done
# print all key
echo ${!dic[*]}
# print all value
echo ${dic[*]}
using ./ run :
➜ ./get_ip_dic.sh
./get_ip_dic.sh: line 6: declare: -A: invalid option
declare: usage: declare [-afFirtx] [-p] [name[=value] ...]
./get_ip_dic.sh: line 11: 10.114.12.26: syntax error: invalid arithmetic operator (error token is ".114.12.26")
but, using bash run :
➜ bash get_ip_dic.sh
10.134.34.228 10.134.34.227 10.114.12.27 10.114.12.26 10.129.35.188
b5be4856d837 2b8b028e6eeb b5be4856d837 b5be4856d837 2b8b028e6eeb
After searching online, I know that both methods are run in the subshell,there isn't different.
So,I don't know what happened,thanks in advance.
./get_ip_dic.sh uses the shebang, and so runs the script using /bin/bash. I'll assume you are on macOS, where /bin/bash is version 3.2.56, which doesn't support associative arrays.
bash get_ip_dic.sh, on the other hand, runs whichever bash appears first on your search path, which would appear to be a newer version of bash that you installed yourself.

awk and bash script? [duplicate]

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

Syntax error when using sudo to execute a script [duplicate]

This question already has answers here:
Difference between sh and Bash
(11 answers)
Closed 4 years ago.
I encounter a problem when launching a script using sudo, though I have no problem and the script works fine without sudo.
The Line is :
mapfile -t dataList< <( tac /tmp/result.log | grep 'Command' | cut -d" " -f1 )
The error is "Syntax error near the unexpected symbol " < ".
The sudo command is :
sudo -u victor /tmp/parse.sh
Thank you all for help...
Sounds like a different shell is executing your script, one which doesn't understand the used syntax.
Your script /tmp/parse.sh might lack the #!/bin/bash (or similar) in its head line, and a different shell (root's login shell?) might be used to execute it.
This could be fixed by adding the missing #! line in the script header (recommended), or by calling the shell explicitly:
sudo -u victor bash /tmp/parse.sh

Resources