Operating System - Linux
1758844 Members
2767 Online
108876 Solutions
New Discussion юеВ

Echo PIPE character which stored in variable

 
SOLVED
Go to solution
Eric Leung_2
Advisor

Echo PIPE character which stored in variable

Assuming that:
$*= "rdo ls -l /tmp | grep new"

The shell script:
#!/bin/ksh
CMD=`echo $* | cut -f1- -d " "`
printf "%s: [%s]> %s\n" "`date`" $LOGNAME "${CMD}" >> /root/rdo.log
${CMD}

The above script refers. I wanted to write the $* into /root/rdo.log. However, I got the following output:

Thu Aug 18 09:45:54 HKT 2005: [root]> ls -l /tmp

The "| grep new" was lost.

I think that it was because the "| grep new" was interpreted as part of the printf command. What change is necessary so that the whole command can be recorded correctly?

Thanks
9 REPLIES 9
Ermin Borovac
Honored Contributor

Re: Echo PIPE character which stored in variable

It's probably because arguments to the shell scripts are not quoted.

For example if you run your shell script as

$ ./test.sh rdo ls -l /tmp | grep new

your shell script will only see the part up to the pipe symbol (that's what gets logged).

However if arguments to your shell script are passed in single or double quotes as

$ ./test.sh 'rdo ls -l /tmp | grep new'

then the whole command will be logged correctly. However pipe symbol will lose its special meaning (because of quotes) so the last line of the script should be changed to

eval ${CMD}

for the shell to re-evalute the command and reassign the special meaning to the pipe symbol.
Muthukumar_5
Honored Contributor

Re: Echo PIPE character which stored in variable

You can try as,

#!/bin/ksh
CMD=`echo $* | cut -f1- -d " "`
printf "%s: [%s]> %s\n" "`date`" $LOGNAME "${CMD}" >> /root/rdo.log
echo ${CMD} | ksh

Pass arguments with " (double quote) or with ' (single quote).

hth.
Easy to suggest when don't know about the problem!
Eric Leung_2
Advisor

Re: Echo PIPE character which stored in variable

Thanks Experts.

Actually, the problem is at the printf statement.

In fact, this script is called by C program, which can successfully pass the "rdo ls -l /tmp | grep new" to this shell script and the command "ls -l /tmp | grep new" can also run as expected.

However, the /root/rdo.log can only log "ls -l /tmp" instead of what I want "ls -l /tmp | grep new".

Any other idea?
Ermin Borovac
Honored Contributor

Re: Echo PIPE character which stored in variable

I might be wrong but can you please try the following.

To see if arguments passed to the script are quoted or not you can insert the following code at the top of the script

i=1
for arg in "$@"
do
echo "$i: $arg" >> /tmp/test.log
((i=$i+1))
done

If arguments passed to the script were quoted you will see everything on one line

1: rdo ls -l /tmp | grep new

If not, you will see something like

1: rdo
2: ls
3: -l
4: /tmp

Eric Leung_2
Advisor

Re: Echo PIPE character which stored in variable

Wow! You are right! The "| grep new" was not printed in the log file. That proved that the "| grep new" was not part of the arg.

By the way, how come the last statement behaves as I expected? (i.e. the "| grep new" is executed).
Ermin Borovac
Honored Contributor
Solution

Re: Echo PIPE character which stored in variable

Well, if you run it from command line without quotes as

$ ./test.sh rdo ls -l /tmp | grep new

shell first runs './test.sh rdo ls -l /tmp', then it passes the output of that to 'grep new'.

This is probably what happens in your case. Does that clear it up?
Eric Leung_2
Advisor

Re: Echo PIPE character which stored in variable

For your information, my C program (rdo.c)is as simple as below:

main(int argc, char *argv[])
{
setuid(0);
setgid(0);
execvp("/usr/sbin/shell.sh", argv);
exit(1);
}

I use it as follow:
rdo ls -l /tmp | grep new

The output of this command is = "ls -l /tmp/ | grep new"
Eric Leung_2
Advisor

Re: Echo PIPE character which stored in variable

I understand the whole situation now.

Thank you very much
Ermin Borovac
Honored Contributor

Re: Echo PIPE character which stored in variable

Looking at your C code it looks to me that you have a gigantic security hole on your system. I strongly suggest you look into installing and using sudo.

http://www.courtesan.com/sudo/