How can I send the input of a read line command through a shell script

Hi All,

I wish to automate the unix command 'su' through a shell script.

I would like to pass the content of a file as password to 'su' command.

My script is as below,

#! /bin/sh

su userA

while read line
do
rpm -ivh $line
done < pwd.txt

where pwd.txt contains the password of the userA.

please help me how on how to do this.
I have tried in some other way as mentioned in this Forum.
But I couldn't do this. please help me out.. Thank you all in advance,..:b:

I'll check it. Why you wanna do this anyway? Can't you just use root account or something.

Hi ryandegreat25,, Thanks for your response...

I have written my shell script in user A.. I have provided that script for userB.

I would like to provide only the execute permission to userB..

In such a case, I can't run my script as UserB without providing read permission.

so, I would like to automate a script to switch to User-A to change the mode of my script to deny read/write permissions.

Sudo is not working in my server. If you know any other way kindly help me to do so..

Anyhow, Thank you so much for your response.. :b:

haven't been able to accomplished this. Can't see any discussion done this neither.

cat /etc/sudoers
<ID> ALL=NOPASSWD: ALL

You can check the NOPASSWD in the sudoers file, but i'll tell you it's a BIG security risk.

The best solution for your problem is to use sudo. So try to find out why sudo is not working for you.

Assume that you are userA and you want to give only execute option to userB to execute script /home/UserA/MyScript.sh. In suders you can configure like below

UserB  ALL=(UserA)   NOPASSWD: /home/UserA/MyScript.sh

And UserB is supposed to execute the script like below.

sudo -u UserA /home/UserA/MyScript.sh

The script will be run as UserA other than that I don't think there any security risk, if you do it like this.

Hi ranjithpr, Thanks for your answer.

But, the sudo command is restricted in my server.

Can you pls brief me how can i configure suders. where the suders available.

Meanwhile, I have tried to secure my script by encrypt them. but still i have problem with executing the encrypted script.

can anyone explain me, how to execute the encrypted file.

Thank you all in advance...:b:

Only Administrator(root) can configure sudo,

You can add entries in /etc/sudoers using command visudo which is in directory /usr/sbin

Login as root and add configuration in /etc/sudoers using visudo

I have already mentioned how to add configuration and how to execute script.