Run script as sudo and read the password from passwd file

I have a script that can only run when using  the sudo command and  password.  I need to read the sudo user's password from etc/passwd  file without getting prompted for a password.

Here is my code:

The  code remove ppids  and orphans from sasworks directory.

#!/bin/bash
 
cleanwork= 'sudo -S apt-get update <~/passwd.txt  /sas/sashome/sasfounadation/utilities/bin/cleanwork /sasworks' 

./cleanwork

cleanwork93= 'sudo -S apt-get update <~/passwd.txt  /sas93/sashome/sasfounadation/utilities/bin/cleanwork  /sasworks'

./cleanwork93

Open in new window


 sasadm is a sudo user and is the only user  who can run the code.  I donot need to add sasadm to the script.  The password for sasadm is in the etc/passwd,txt. The  password  need to be encrpyted.

passwd.txt content is:
w12345
dfn48Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

woolmilkporcCommented:
Why don't you add the "NOPASSWD" option to sasadm's sudoers entry (the one allowing execution of "apt-get" or rather "cleanwork")?

There is no easy way of feeding encrypted passwords to sudo.

Besides that, the script doesn't actually look as if it could work.
What are you trying to achieve by setting a variable to a string of commands and then trying to execute a file having the same name as the variable?

Next, the equal sign in variable assignments must not be surrounded by spaces.

Do you rather think of something like this:

#!/bin/bash
 
cleanwork='sudo -S apt-get update <~/passwd.txt  /sas/sashome/sasfounadation/utilities/bin/cleanwork /sasworks'

$cleanwork

cleanwork93='sudo -S apt-get update <~/passwd.txt  /sas93/sashome/sasfounadation/utilities/bin/cleanwork  /sasworks'

$cleanwork93

?

Finally. why do you run "apt-get" when your true goal is running a SAS program?

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
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.