鉴于以下shell脚本,如何重新排列输出以插入新行?
#!/bin/bash
echo -e $1 ` date `
我尝试了-e开关,但无济于事。
我的意思是如果我输入:./script.sh "
ls -la "
我得到以下输出:
total 4732 drwxr-xr-x 4 root root 4096 2013-01-09 15:32 . drwx------ 28 root root 4096 2013-01-09 15:32 .. -rw-r--r-- 1 root root 484063 2013-01-06 05:22 Blackhat-europe-09-Damele-SQLInjection-whitepaper.pdf -rwxrwxrwx 1 root root 3787587 2013-01-06 05:10 FoxitReader-1.1.0.tar.bz2 -r-------- 1 root root 127693 2013-01-03 22:55 HackYeah-SQL-Injection.pdf drwxr-xr-x 2 root root 4096 2013-01-05 21:47 Inst_DVWA -rw-r--r-- 1 root root 3223 2012-12-22 00:47 nmap-O -rw-r--r-- 1 root root 284 2013-01-06 07:31 payload_dvwa.txt -rwxrwxrwx 1 root root 30 2013-01-09 11:18 script (copy).sh -rwxrwxrwx 1 root root 33 2013-01-09 15:32 script.sh -rw-r--r-- 1 root root 395493 2013-01-06 05:03 SQLmap_README.pdf -rwxrwxrwx 1 root root 705 2013-01-07 15:22 start_dvwa.sh -rw-r--r-- 1 root root 834 2013-01-06 07:31 Unsaved Document 1.txt drwxr-xr-x 2 root root 4096 2012-12-19 18:47 ZAP_certificate Wed Jan 9 15:32:55 CET 2013
我想获得类似的输出:
total 4732
drwxr-xr-x 4 root root 4096 2013-01-09 15:32 .
drwx------ 28 root root 4096 2013-01-09 15:32 ..
-rw-r--r-- 1 root root 484063 2013-01-06 05:22 Blackhat-europe-09-Damele-SQLInjection-whitepaper.pdf
-rwxrwxrwx 1 root root 3787587 2013-01-06 05:10 FoxitReader-1.1.0.tar.bz2
-r-------- 1 root root 127693 2013-01-03 22:55 HackYeah-SQL-Injection.pdf
drwxr-xr-x 2 root root 4096 2013-01-05 21:47 Inst_DVWA
-rw-r--r-- 1 root root 3223 2012-12-22 00:47 nmap-O
-rw-r--r-- 1 root root 284 2013-01-06 07:31 payload_dvwa.txt
-rwxrwxrwx 1 root root 30 2013-01-09 11:18 script (copy).sh
-rwxrwxrwx 1 root root 33 2013-01-09 15:32 script.sh
-rw-r--r-- 1 root root 395493 2013-01-06 05:03 SQLmap_README.pdf
-rwxrwxrwx 1 root root 705 2013-01-07 15:22 start_dvwa.sh
-rw-r--r-- 1 root root 834 2013-01-06 07:31 Unsaved Document 1.txt
drwxr-xr-x 2 root root 4096 2012-12-19 18:47 ZAP_certificate
非常感谢
答案 0 :(得分:0)
尝试
echo -e "`$1`\n`date`"
引号应保留换行符。
答案 1 :(得分:0)
尝试
#!/bin/bash
$*
date
我不确定./script "ls -la"
对ls -la; date
的改进程度,但我认为你有理由。
作为关键的可用性改进,我宁愿./script ls -la
没有引号;只需为"$@"
切换$*
即可。然后,除其他外,传入引用的字符串作为参数也将起作用。例如,./script perl -le 'print 0x12, "foo"'