Passing a variable to sudo sed command

hi,

dataParse(){
line="$@"
name="cat /etc/passwd | grep "$line": | cut -f6 -d':'"
eval $name > sam.txt 2>&1

sudo -u $line sed -n 's/data-1/&/p' $name/test.xml >> sam1.txt

}

Here i getting the homedir of the accounts and is set in
name variable.which returns "/home/raju" which i am saving it in sam.txt. This works fine. But when i try to pass this $name variable to " sudo -- sed" command it is not taking and hence the sudo command is not executed.
Pls. do provide a soln to this.

thanks

This looks a bit broken to me ?
You've got unescaped quotes in the name= line, and you seem to be trying to use the cat... commandline stored in name as part of a filepath in the sudo line... You're also running a farily dangerous grep as you arn't being specific as to which field in the password file to search (so someone with an odd shell or home dir could match incorrectly).

Not understanding what you're after, my guess may be off base, but try something like this to get closer:

uname=$1
homedir=`egrep "^${uname}:" /etc/passwd | cut -d ':' -f 6`
echo $uname > sam.txt    # But don't know why
sudo -u $uname "sed -n 's/data-1/&/p' ${homedir}/test.xml >> sam1.txt

If you elaborate on what you're after exactly, we can probablt offer more advice :wink:

Thanks Dragon..It works. U made me smile...

:smiley: Pleased to help