1

Ok if subject is not clear i will explain my doubt.I have a shell script kept at my local user called (executeAdM.sh), and when i execute this script i am connecting to SUDO user by taking commands from instruction file.But when i execute this script i am also passing parameter to this script which is actually some directory path of SUDO user. See the script below-:

Script at local(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

Executing Above as-:

./executeADM.sh somePath

When above script executes it connects successfully to another user, and at SUDO user i execute a for loop which search for another SCRIPT called testingADM.sh . Once the script is found i execute that script with parameter passed from Local User and testingADM.sh should set that path for me which is not working it is not reading prameter passed from local user.Can anyone help me here i am not getting how to solve this. Please ask me if anything is not clear basically i want to pass parameter from LOCAL shell script to SUDO USER shell script-:

SUDO user script (testingADM.sh)-:

#!/bin/bash
path=$1
cd $path
pwd

If i hard code path variable value in script kept at SUDO all works fine . But i don't want that.:

 #!/bin/bash
 path=somepath
 cd $path
 pwd

1 Answers1

2

You have more problems than you think. Firstly, you can pass a parameter to a sudo'ed script no problem:

[me@nagios ~]$ cat parent.sh 
#!/bin/bash
echo parent has $1 $2
sudo ./child.sh $1 $2
[me@nagios ~]$ cat child.sh 
#!/bin/bash
echo child has $1 $2
[me@nagios ~]$ ./parent.sh a1 b2 c3
parent has a1 b2
child has a1 b2

Your problem is you're using a here document (<<EOF), which is not a mechanism that allows for the passing of parameters in an easy way: rewrite to have one script invoke another, and you should be fine.

You're also looping over the output of ls, which as many people have noted around here is a terrible idea, and will break as soon as you get a sufficiently unusual filename. Just test for the existence (or better, the executable existence, with [ -x) of testingADM.sh before invocation.

Basically, you've written yourself an XY problem: as soon as you stop trying to do things in the wrong way, many of your issues will go away.

MadHatter
  • 78,442
  • 20
  • 178
  • 229
  • it worked with EOF if changed \EOF to just EOF and remove all conditions (For and if) . Can you explain on that? – Tushar Sharma Aug 04 '17 at 12:15
  • 1
    @TusharSharma He gives you some suggestions in addition to what I explained in my answer over on SU (@MadHatter, this one was cross-posted). Please read http://mywiki.wooledge.org/ParsingLs on why there is no need need for using `ls` to iterate over filenames. You could use the `for` loop on all files, but even then, you don't actually need to loop over all files to just get a single one. Just call the file directly. – slhck Aug 04 '17 at 12:35