我想将 ls 命令的输出捕获到一个文件中
ls >> lsOutput.log
Run Code Online (Sandbox Code Playgroud)
如果在命令行中执行,则此方法有效。但是当放入 shell 脚本 ( lsOutput.sh
) 时,返回
./lsOutput.sh: 3: ./lsOutput.sh: total: not found
Run Code Online (Sandbox Code Playgroud)
lsOutput.sh
代码
#!/bin/sh
`ls -lrt` >> lsOutput.log
Run Code Online (Sandbox Code Playgroud)
aul*_*ron 10
只需从脚本中删除反引号:
#!/bin/sh
ls -lrt >> lsOutput.log
Run Code Online (Sandbox Code Playgroud)
否则,执行命令,然后替换并执行其输出。
例如:
echo date
Run Code Online (Sandbox Code Playgroud)
将输出:date
, 而
`echo date`
Run Code Online (Sandbox Code Playgroud)
将输出当前日期,即它将首先评估为date
,然后执行,这将调用程序date
本身。