- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: Echo PIPE character which stored in variable
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-17-2005 02:27 PM
тАО08-17-2005 02:27 PM
$*= "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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-17-2005 09:24 PM
тАО08-17-2005 09:24 PM
Re: Echo PIPE character which stored in variable
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-17-2005 11:13 PM
тАО08-17-2005 11:13 PM
Re: Echo PIPE character which stored in variable
#!/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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-18-2005 01:55 PM
тАО08-18-2005 01:55 PM
Re: Echo PIPE character which stored in variable
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-18-2005 02:22 PM
тАО08-18-2005 02:22 PM
Re: Echo PIPE character which stored in variable
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-18-2005 02:45 PM
тАО08-18-2005 02:45 PM
Re: Echo PIPE character which stored in variable
By the way, how come the last statement behaves as I expected? (i.e. the "| grep new" is executed).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-18-2005 02:49 PM
тАО08-18-2005 02:49 PM
Solution$ ./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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-18-2005 02:51 PM
тАО08-18-2005 02:51 PM
Re: Echo PIPE character which stored in variable
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"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-18-2005 02:55 PM
тАО08-18-2005 02:55 PM
Re: Echo PIPE character which stored in variable
Thank you very much
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-18-2005 03:05 PM
тАО08-18-2005 03:05 PM
Re: Echo PIPE character which stored in variable
http://www.courtesan.com/sudo/