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?