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

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

Related

How to read from a file in bash script? [duplicate]

This question already has answers here:
Looping through the content of a file in Bash
(16 answers)
Closed 10 months ago.
I have a simple bash script below that outputs into a file threat info from the domain 1605158521.rsc.cdn77.org. The domain is read from B1Dossier.
#!/bin/bash
baseurl=https://csp.infoblox.com
B1Dossier=/tide/api/data/threats/state/host?host=1605158521.rsc.cdn77.org
APIKey=<REDACTED>
AUTH="Authorization: Token $APIKey"
curl -H "$AUTH" -X GET ${baseurl}${B1Dossier} > /tmp/result
This time, I want the script to get information from multiple domains. For example, I have a file (domfile) with the following domains with each being on a new line:
cdn.js7k.com
example.org
www.hdcctvddns.com
How can I turn my script to execute on each domain from a file (domfle)?
you can use a for loop something like this:
while read -r line; do
echo "$line"
done < <file_path>
you can also use cat to read the file, and use xargs you can execute any command you want per line. but if used incorrectly can lead to command injection.
If for some reason you need to use this syntax over the for loop. take a look at the comments, and research more about command injection with xargs sh.
cat <file_path> | xargs ...

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

SSH Remote Store Command Result In Variable [duplicate]

This question already has answers here:
How to cat <<EOF >> a file containing code?
(5 answers)
Closed 7 years ago.
I'm trying to run a bash script that ssh's onto a remote host and stops the single docker container that is running.
#!/usr/bin/env bash
set -e
ssh <machine> <<EOF
container=$(docker ps | awk 'NR==2' | awk '{print $1;}')
docker stop $container
EOF
However, I get the following error:
stop.sh: line 4: docker: command not found
When I do this manually (ssh to the machine, run the commands) all is fine, but when trying to do so by means of a script I get the error. I guess that my command substitution syntax is incorrect and I've searched and tried all kinds of quotes etc but to no avail.
Can anyone point me to where I'm going wrong?
Use <<'EOF' (or <<\EOF -- quoting only the first character will have the same effect) when starting your heredoc to prevent its expansions from being evaluated locally.
BTW, personally, I'd write this a bit differently:
#!/bin/sh -e
ssh "$1" bash <<'EOF'
{ read; read container _; } < <(docker ps)
docker stop "$container"
EOF
The first read consumes the first line of docker ps output; the second extracts only the first column -- using bash builtins only.

Sudo throws error while reading a file post changing the user [duplicate]

This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Escaping a dollar sign in Unix inside the cat command
(2 answers)
Useless use of cat?
(9 answers)
In a PHP / Apache / Linux context, why exactly is chmod 777 dangerous?
(4 answers)
Closed 3 years ago.
Logging into the shell as a normal user and I am hitting a DB and storing the output to a file. Then within the script and changing into root user(sudo su) and then trying to access the file. But am getting error.
pro=$(sqlplus <code goes here>)
echo $pro > pro.txt
chmod 777 pro.txt
sudo su <<Here
cat pro.txt
total=$(cat pro.txt|tr -cd ' \t' | wc -c)
echo $total
i=1
provide=$(cat pro.txt|awk -v var=$i '{print $var}')
Output received :
no output came for "echo $total"
last line get error as "bash: line 10: azure: command not found"
The same script got executed when i didnt get changed to root user.
It is not clear what you are trying to do or where the azure text is coming from; but my understanding is that the text comes from the output of the SQL command and then gets evaluated because you are not quoting it properly.
See When to wrap quotes around a shell variable? or simply submit your code to http://shellcheck.net/ before asking for human review.
The following attempts to fix the broken quoting, the incorrect permissions, and the multiple useless cats.
# Don't capture to a variable; simply write directly to a file
# This also coincidentally removes one instance of broken quoting
sqlplus <code goes here> >pro.txt
# Don't use insecure permissions
chmod 644 pro.txt
# Add missing Here terminator at the end; backslash causes code to be run by su
sudo su <<\Here
cat pro.txt
# Avoid useless cat
total=$(tr -cd ' \t' <pro.txt| wc -c)
# Fix quoting
echo "$total"
i=1
# Fix quoting and useless cat
provide=$(awk -v var="$i" '{print $var}' pro.txt)
# Add missing Here terminator
Here
This code doesn't do anything useful because the variables will be gone when the sudo su subshell exits, so there is probably more work to be done. Perhaps ask a new question where you explain what you hope to actually accomplish.
A particularly pesky problem was the lack of quoting around the here document terminator. Without it, the command substitutions would be executed by the current shell. Let me demonstrate:
sudo su <<Here
echo $(who am i)
Here
The output is you, not root, because the command substitution $(who am i) is evaluated by your current shell before sudo su runs. Quoting or backslashing the string after << disables this expansion, and passes the entire here document verbatim as standard input to sudo su. See also Escaping a dollar sign in Unix inside the cat command

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

Resources