-1

给定以下 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

非常感谢

4

2 回答 2

0

尝试

#!/bin/bash
$*
date

我不确定如何./script "ls -la"改进,ls -la; date但我想你有你的理由。

不过,作为一项重要的可用性改进,我更喜欢./script ls -la不带引号;只需切换"$@". $*然后,除其他外,将带引号的字符串作为参数传递也将起作用。例如,./script perl -le 'print 0x12, "foo"'

于 2013-01-09T15:36:23.190 回答
0

尝试

echo -e "`$1`\n`date`"

引号应保留换行符。

于 2013-01-09T14:55:49.633 回答