1

I have a shell script kept at my local user called (executeAdM.sh), and when I execute this script I am switching to a sudo user by taking commands from an instruction file. But when I execute this script I am also passing parameters, which is actually some directory path of the sudo user. See the script below.

Script for local user (executeADM.sh):

#!/bin/bash
echo password | sudo -S -l
sudo /usr/bin/su - user <<\EOF
#ls -lrt
pwd
for entry in $(ls -r)
 do
 if [ "$entry" = "testingADM.sh" ];then
 ./"$entry" "1ドル"
 fi
done
EOF

I'm executing the above as:

./executeADM.sh "/global/u70/globe/ADM"

When the above script executes, it switches successfully to the other user, and with the sudo user I execute a for loop which searches for another script called testingADM.sh.

Once the script is found I execute that script with the parameter passed from the local user, and testingADM.sh should set the path for me.

This is not working – it is not reading the parameter passed from the local user.

The sudo user script (testingADM.sh):

#!/bin/bash
changepath=1ドル
cd "$changepath"
pwd

When I hardcode the path variable in the script, all works fine. But I don't want that:

 #!/bin/bash
 changepath=somepath
 cd "$changepath"
 pwd

The issue is instead of going to "/global/u70/globe/ADM" it takes the path as "/global/u70/globe/", Which is the path after I do sudo. It is just not passing the variable.


With the below suggestions, this is what I ended up with:

#!/bin/bash
echo password | sudo -S -l
sudo /usr/bin/su - user <<EOF
#ls -lrt
#pwd
echo "1ドル"
./testingADM.sh "1ドル"
EOF

The echo command prints nothing; a blank space is shown. It worked when I changed \EOF to EOF.

Can anyone explain the difference between:

\EOF , "EOF" , 'EOF' , EOF

or do the first three have the same meaning? Then what is difference between them?

slhck
236k73 gold badges639 silver badges611 bronze badges
asked Aug 4, 2017 at 6:05
4
  • Please don't change your question after you received an answer that solves your problem. Once a question is answered, it's confusing if it gets changed or has additions later. If you have further questions, it's better to solve them in the comments or ask an entirely new question. Commented Aug 6, 2017 at 11:18
  • @slhck got your point so now can you tell me the reason in comment section? Commented Aug 6, 2017 at 11:20
  • I'm not sure I can answer this without some specific examples, seeing what you've tried and why it didn't work for you. So please ask a new question about it. Thanks! Commented Aug 6, 2017 at 11:22
  • Okk no issues i will post a new question. Commented Aug 6, 2017 at 11:23

2 Answers 2

2

Do not quote or escape your heredoc limit string (i.e. use EOF instead of \EOF or "EOF"), as otherwise, special variables like $ will not be interpreted. See here for more info and examples (e.g. Example 19-7).

Here is a minimal script that works:

$ cat test.sh
#!/bin/bash
sudo su - $(whoami) <<EOF
./test2.sh "1ドル"
EOF
$ cat test2.sh
#!/bin/bash
foo=1ドル
echo "$foo"
$ ./test1.sh "/path/to/file"
"/path/to/file"

Note that:

  • There is no need for the heredoc at all. Just call ./test2.sh directly after sudo: sudo -u $(whoami) test2.sh "1ドル"
  • You should not loop over the output of ls.
  • There is no need to do a for loop to search for a single file. Just call the script directly with its name.

You should also quote your variables, that is:

  • Execute your script with ./executeADM.sh "/path/with white/space"
  • Call ./"$entry" "1ドル" in your script
  • In your other script, use cd "$path"

In general:

  • Always double-quote variables. (Don't single-quote them -- they won't expand.)
  • Don't use a variable called path. (Much as you wouldn't use variables called user, home, etc.)
answered Aug 4, 2017 at 8:19
19
  • i updated my answer with path i am providing. Commented Aug 4, 2017 at 10:27
  • So when you echo "$changepath", it is /global/u70/globe/ and not /global/u70/globe/ADM? That's quite strange. Commented Aug 4, 2017 at 10:44
  • @slhck: that is weird, indeed. Commented Aug 4, 2017 at 10:50
  • @slhck exactly mate is it due to passing parameter from local and reading after sudo with EOF? May be it is not sending parameter ?? Commented Aug 4, 2017 at 10:56
  • @tukan yes indeed it is , and i think EOF is not reading parameter. Commented Aug 4, 2017 at 10:57
1

Edit
From back of my head. Does your path contain spaces?

If yes you need to execute it as ./executeADM.sh 'somePath'.

It is always better to save the 1ドル into a variable when you are passing it further it makes it easier to read. I would add path=1ドル then in batch file you would have ./$entry "$path"

answered Aug 4, 2017 at 8:02
6
  • ./$entry '$path' will not expand the variable but pass it literally as $path. Commented Aug 4, 2017 at 8:09
  • @slhck: you are right. I've edited the answer to have double quotes - for expansions. The first path can be with single quotes there you don't need any expansion. Commented Aug 4, 2017 at 10:45
  • Although as you can see in the OP's update to the question, that is not the problem. Commented Aug 4, 2017 at 10:46
  • @slhck: ah missed that, will check it out. Thanks for the update. Commented Aug 4, 2017 at 10:48
  • @tukan thanks for your help and time as well sorry I responded late. Commented Aug 4, 2017 at 18:36

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.