Run shell script program as sudo

My shell script will not run without errors. I have tried many different ways in implementing the script and
nothing appears to work.   In my code, Sudo runs cleanwork.  In order for cleanwork to execute
 it needs to point to a tmp or a work directory that has long running processes that are called orphans.  Those orphans
need to be remove because of disk space.

cleanwork is not a directory.

cleanwork is not  in the same path as sasworks directory.  This code run successfully using the command line but not in a shell script.
I am prompted for a password. I typed the password in and I get error messages which are as follows:  sudo: no command found, cleanwork: no file    and saswork directory does not exist.  

Here is the code:
 WORK='/saswork'
 cleanwork92='sudo   /sasem/sas92/sashome/sasfoundation/9.2/utilities/bin'
 
  echo  $cleanwork92

  sudo ./cleanwork $WORK

   cleanwork94='sudo   /sasem/sas94/sashome/sasfoundation/9.4/utilities/bin'
   echo $cleanwork94
  
  sudo ./cleanwork  $saswork

 

Open in new window

dfn48Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
How did you invoke the shell script?
Was "sudo: no command found, cleanwork: no file    and saswork directory does not exist." a single error message, or was it several different messages from different commands?
If it was several different messages, what were the individual messages, and what were the individual commands that caused them?
Did it really say "cleanwork" or "./cleanwork"?
Did it really say "saswork" or "/saswork"?
Was $saswork defined?  If so how was it defined?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
simon3270Commented:
It would make it easier for us to spot the problem if you added

    set -x

to the script. before the "WORK=" line, then show us the output from that.

I'm a bit confused by the lines like:
    cleanwork92='sudo   /sasem/sas92/sashome/sasfoundation/9.2/utilities/bin'
They seem to be setting a variable to the string "sudo" followed by a path to what looks like a directory containing programs or scripts. Are you trying to change to that directory, before running the cleanwork program there?  If so:
    cd /sasem/sas92/sashome/sasfoundation/9.2/utilities/bin
    sudo ./cleanwork /path/to/tmp/files/
0
simon3270Commented:
Just spotted an earlier question of yours, and now see what the cleanwork92 variable is for (and how you have ended up with the script you have!).

You had:
    cleanwork92='/usr/bin/sudo   /sasem/sas92/sashome/sasfoundation/9.2/utilities/bin/ cleanwork /$saswork'
    $cleanwork92
That's an unwieldy way of running a script, but there are also some problems with it
- you have a space between /bin and cleanwork - remove that, and the sudo command will run the right script
- you use /$saswork, but you haven't defined $saswork, only $WORK (which is set to "/saswork".  So either use $WORK (note, in capitals), or have /saswork in your command.
- the $saswork variable would not be replaced by the text it was set to, so the string "/$saswork" would be passed as the parameter to cleanworks.  If you want the shell to evaluate variables in strings like this, put "eval" before the command.
- alternatively, if you use double quotes when assigning cleanwork92, the variable replacement will take when actualy setting the value of $cleanwork92.

So, the two lines become
    cleanwork92='/usr/bin/sudo  /sasem/sas92/sashome/sasfoundation/9.2/utilities/bin/cleanwork $WORK'
    eval $cleanwork92
or
    cleanwork92="/usr/bin/sudo  /sasem/sas92/sashome/sasfoundation/9.2/utilities/bin/cleanwork $WORK"
    $cleanwork92
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.