Shellscript to Sudo other account

Hi,

I have to write a shell script to sudo other account. Foreg,
i am working in account ramdev1, i have a sudo access where through shell script i need to sudo another account
ramdev2 and read a file "ram.inc" get those data and save it in a .txt file and return it?

Since i am new to shell script. please do provide a soln. to this. This is really urgent. Expecting positive response.

Thanx,
Sachin

Search 'sudo' against the forum.

cameron,

I have used "su" to sudo an account ramdev2, now i need to read a file "ram.inc" in a folder /ramenv ,parse it and get only the port no. in it and store it in a .txt file and return it. Since i am pretty much new in reading the file and storing it in a .txt pls. do provide me with an example. Expecting postive response..

Thanx,
Sachin

If you used 'su' then you did not sudo. Anyway... for parsing a file, suggest you read through 12 ways to parse a file. That should be a good start. Then regarding storing a variable in a file, go through the man page of any shell (sh/bash/ksh) and search for redirection. That will help too.

hey,

su is used to switch user account.
For ex:
By default you logged in into default user, but whn you want to switch the user (say your account ) then you can use "su" command,

su - xyz
password: *******

:b:

Would be interested in seeing your script.
Expecting postive response..

Cheers,
Cameron

blowtorch,

Thank u for the response..I am a Java/J2EE guy. Where i have been a given a shell script to write. Since i am new to this and since you are the moderator can you help me with an example shellscript to sudo other account and
read a file "ram.inc" and get only the port no. in it and store it in ram.txt file and return it. Since this is urgent to be delivered can provide me with an example atleast.
So that i can modify it according to my need.

Thanks,
Sachin

Here is my requirement to be more elaborate:-

I have a ram.inc file in account ramdev2:-

<username> ram </username>
<password> tttt </password>
<port-no> 1000 </port-no>
<data1> hhh </data1>
<data2> ttt </data2>

By using sudo -u ramdev2 i need to connect to the ramdev2 account and find the file "ram.inc" parse it and get only the port-no (which is 1000) and save this value in a .txt file and return it. All these needs to be done through a shell script. Please do provide a solution for this. I will be really thankful to u guys. It is really urgent.

Thanks,
Sachin

Please do provide an answer...Since it is really urgent.

This is not encouraged in the forum.

Please read the rules of the forum

Sorry Madhan, I dont have any other choice where i need
to deliver this today. Since i am pretty new to shell scripts i am finding it difficult. Sorry if i have written anything wrong.
I read some of the suggestions given by you, pls. do provide a solution for this with an example. It will be really helpful.

try this,

sudo -u ramdev2 sh -c " sed -n '/port-no/s/\(.*>\)\(.*\)\(<\/.*$\)/\2/p' inputfile > outputfile "

Madhan,

Using the command i am not able to get the port no. value in output file. I tried with few other options where nothing worked. Pls. do provide a solution.

I thought I gave that ! :slight_smile:

just try this,

sudo -u ramdev2 sh -c " sed -n '/port-no/s/\(.*>\)\(.*\)\(<\/.*$\)/\2/p' inputfile "

and post the output that you get !

The above one-liner is based on the sample file that is provided

First of all thanks Madhan, i am able to get the output for a single keyword search(listen-port) .If i want to get the data for multiple keyword search what to do?
For eg,

sed -n 's/port-no/&/p' db.xml > example.txt

this works.

But when i try to search for 2 keywords like:-

sed -n 's/port-no/&/dbname/p' db.xml > example.txt

iam getting an error saying :-

"command garbled:"

How to find the data for 2 keywords(port-no and dbname) in the same db.xml?

Madhan, your reply for this..

Here is my shot,

are you interested in all the data points along with the matching data header

something like,

sudo -u ramdev2 sh -c " sed -n 's/<\(.*\)>\(.*\)\(<\/.*$\)/\1-\2/p' inputfile "

this will give output something like,

username- ram
password- tttt
port-no- 1000
data1- hhh
data2- ttt

Madhan, Once again thanks i have written a shellscript
which gets the data from various files for a respective
name and append those data's in a sample.txt file. Here it is:-

parseData(){
param="$@"
sudo -u $param ksh -c sed -n 's/data-1/&/p' /home/$param/example.dat >> sample.txt
sudo -u $param ksh -c sed -n 's/data-2/&/p' /home/$param/example1.dat >> sample.txt

}

FILE="names.txt"
if [ ! -f $FILE ]; then
echo "$FILE : does not exists" exit 1
elif [ ! -r $FILE ]; then
echo "$FILE: can not read"
exit 2
fi
exec 3<&0
exec 0< $FILE
while read line
do
parseData $param
done
exec 0<&3

exit 0

I will be passing names.txt as a command line arg.which contains 10 names . Where each of those names have different HOMEDIR from where we can search the file and get the data. My problem is that i know the HOMEDIR of my name jason( /home/jason/). But i dont know the HOMEDIR for other names. So how i can find the HOMEDIR for other names and for there how can search the example.dat and example1.dat(which is common for all names) and get the respective data?
I have used "~" like "~ /example.dat" but it is getting me the HOMEDIR of the current working directory.
How to solve this problem. One more problem is that when this script is invoked twice, the data's for the same name is appended twice because of ">>". I dont need that where i need to delete the file sample.txt (if it exists), each time when the script is invoked so that i will get only the data for partcular name once. Please do provide a solution for this..

Thanks

what is the value that is populated for $param ?

this should be possible from the file /etc/passwd

for each and every user from /etc/passwd file, home directory of the user can be obtained

If you are sure, that sample.txt file should be created afresh each time the script is run.

Add the following in the start of the script, to check if sample.txt exists and if it is
there delete the file, so that new copy of data would be directed to the file

if [ ! -f sample.txt ]
then
  /bin/rm sample.txt
fi

BTW,

with this,

sed -n 's/data-1/&/p'

what is the intended effect that you wish to have with the above code ?