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?

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.
    – slhck
    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!
    – slhck
    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.)
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.
    – slhck
    Commented Aug 4, 2017 at 10:44
  • @slhck: that is weird, indeed.
    – tukan
    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"

6
  • ./$entry '$path' will not expand the variable but pass it literally as $path.
    – slhck
    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.
    – tukan
    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.
    – slhck
    Commented Aug 4, 2017 at 10:46
  • @slhck: ah missed that, will check it out. Thanks for the update.
    – tukan
    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.

Not the answer you're looking for? Browse other questions tagged .