1

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.

Praveen P
  • 57
  • 5
  • Where is the `azure` command? – tripleee Oct 28 '19 at 07:56
  • @tripleee : I've used 777 just here, code have 755 only. Thanks for that. I am new to unix and please let know the purpose of azure. – Praveen P Oct 28 '19 at 08:00
  • We have no idea. I guess your code is trying to connect to Microsoft's cloud service but there is nothing in your question which reveals where, how, or why this is happening. – tripleee Oct 28 '19 at 08:09
  • Hi Triplee, its not trying to connect to azure. The file which i read have azure at the second place. It is merely reading the file. – Praveen P Oct 28 '19 at 08:10
  • So broken quoting it is then. I will post an answer and mark this as a duplicate. – tripleee Oct 28 '19 at 08:11

1 Answers1

1

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

tripleee
  • 175,061
  • 34
  • 275
  • 318