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?
-
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– slhck2017年08月06日 11:18:53 +00:00Commented Aug 6, 2017 at 11:18
-
@slhck got your point so now can you tell me the reason in comment section?Tushar Sharma– Tushar Sharma2017年08月06日 11:20:45 +00:00Commented 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– slhck2017年08月06日 11:22:21 +00:00Commented Aug 6, 2017 at 11:22
-
Okk no issues i will post a new question.Tushar Sharma– Tushar Sharma2017年08月06日 11:23:19 +00:00Commented Aug 6, 2017 at 11:23
2 Answers 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.shdirectly aftersudo:sudo -u $(whoami) test2.sh "1ドル" - You should not loop over the output of
ls. - There is no need to do a
forloop 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 calleduser,home, etc.)
-
i updated my answer with path i am providing.Tushar Sharma– Tushar Sharma2017年08月04日 10:27:51 +00:00Commented 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– slhck2017年08月04日 10:44:15 +00:00Commented Aug 4, 2017 at 10:44 -
@slhck: that is weird, indeed.tukan– tukan2017年08月04日 10:50:56 +00:00Commented 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 ??Tushar Sharma– Tushar Sharma2017年08月04日 10:56:55 +00:00Commented Aug 4, 2017 at 10:56
-
@tukan yes indeed it is , and i think EOF is not reading parameter.Tushar Sharma– Tushar Sharma2017年08月04日 10:57:53 +00:00Commented Aug 4, 2017 at 10:57
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"
-
./$entry '$path'will not expand the variable but pass it literally as$path.slhck– slhck2017年08月04日 08:09:09 +00:00Commented 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– tukan2017年08月04日 10:45:50 +00:00Commented 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– slhck2017年08月04日 10:46:41 +00:00Commented Aug 4, 2017 at 10:46
-
@slhck: ah missed that, will check it out. Thanks for the update.tukan– tukan2017年08月04日 10:48:21 +00:00Commented Aug 4, 2017 at 10:48
-
@tukan thanks for your help and time as well sorry I responded late.Tushar Sharma– Tushar Sharma2017年08月04日 18:36:57 +00:00Commented Aug 4, 2017 at 18:36