Bash 如何使用&&;(和)作为参数?
在我的脚本中,我希望将第一个参数作为必需的文件名,并将所有其他参数作为shell命令,由Bash 如何使用&&;(和)作为参数?,bash,Bash,在我的脚本中,我希望将第一个参数作为必需的文件名,并将所有其他参数作为shell命令,由&&(and operator)分隔 但是Bash会剪切除第一个命令之外的所有命令 如何修复它 #!/usr/bin/env bash while inotifywait -q -e close_write -e delete $1; do "${@:2}"; du app; done; 解决方案是使用eval命令。Alex,我知道你试图用inotifywait做什么,但是当我尝试时,它只是挂起,什么也不做
&&
(and operator)分隔
但是Bash会剪切除第一个命令之外的所有命令
如何修复它
#!/usr/bin/env bash
while inotifywait -q -e close_write -e delete $1; do "${@:2}"; du app; done;
解决方案是使用
eval
命令。Alex,我知道你试图用inotifywait
做什么,但是当我尝试时,它只是挂起,什么也不做,因为文件在调用时是静态的。看看你想要完成什么。(编译,运行,测量磁盘用法:<代码> App),在它的中间可以简单地编写它,而不是放<代码> NoTyjyWalth//Cord>。
虽然这只是一个基本的黑客在获得你想要的输出。下面的脚本将源文件作为第一个参数(app.cpp
,如果没有给出名称,默认情况下),并将使用.cpp
之前的名称作为g++输出文件名编译代码。您只需简单地尝试获取所需信息:
#!/bin/bash
src=${1:-app.cpp} ## set the source and output (exe) names
out=${src%.cpp}
printf "\n compiling 'g++ %s -o %s'\n\n" $src $out
[ -f $out ] && rm $out ## remove existing exe before builinding
g++ $src -o $out ## build new exe
[ -f $out ] || { ## validate new exe created
printf "error: compilation failed. exiting script.\n\n"
printf "usage: %s source.cpp (default: app.cpp)\n\n"
exit 1
}
[ -e $out ] || { ## validate it is executable
printf "error: file produced from compilation is not executable.\n"
exit 1
}
time ./$out 2>&1 ## compute elapsed time
exesz=$(du -h $out | awk '{print $1}') ## store disk usage (removing name)
## print results
printf "\n Source file: %s\n" "$src"
printf " output file: %s\n" "$out"
printf " size of exe: %s bytes\n\n" "$exesz"
exit 0
示例输出
$ ./watch.sh
compiling 'g++ app.cpp -o app'
Code Running
and being timed.
real 0m0.004s
user 0m0.001s
sys 0m0.002s
Source file: app.cpp
output file: app
size of exe: 16K bytes
$ ./watch . g++ app.cpp -o app
./ CLOSE_WRITE,CLOSE app.cpp
compiling: g++ app.cpp -o app
Code Running (ver. 6)
and being timed.
real 0m0.003s
user 0m0.001s
sys 0m0.002s
app size: 16K
我使用的测试代码是一个简单的cout
:
#include <iostream>
using namespace std;
int main (void) {
cout << "Code Running" << endl << "and being timed." << endl;
return 0;
}
输出
$ ./watch.sh
compiling 'g++ app.cpp -o app'
Code Running
and being timed.
real 0m0.004s
user 0m0.001s
sys 0m0.002s
Source file: app.cpp
output file: app
size of exe: 16K bytes
$ ./watch . g++ app.cpp -o app
./ CLOSE_WRITE,CLOSE app.cpp
compiling: g++ app.cpp -o app
Code Running (ver. 6)
and being timed.
real 0m0.003s
user 0m0.001s
sys 0m0.002s
app size: 16K
我不确定我是否正确地阅读了这个问题,但我想你是在问如何
观看
管道,即重复运行g++app.cpp-o app&&time./app&&du app
。如果您需要这样做,则必须引用&&
,这样它就不会由您的(顶级)shell解释,而是由watch
调用的shell解释:
watch g++ app.cpp -o app '&&' time ./app '&&' du app
如果您愿意,您当然可以使用反斜杠转义(
\&\&
而不是'&&
)。请举个例子,并添加一些代码。什么?!你能举个例子吗?当inotifywait-q-e close_write-e删除$1;执行“${@:2}”;du-app;完成/看。g++app.cpp-o应用程序和时间。/app和duapp@AlexUshakov到底是什么问题?你预计会发生什么?会发生什么?@AlexUshakov什么?“问题”是什么?很高兴你发现了,但要知道它可能有副作用。我不认为它们在这里是适用的。你能解释一下你的意思是什么副作用吗?当然,请考虑编辑你的答案以包含更多的信息(例如你使用了<代码> EVA/COD>命令)的例子。谢谢你的回复。据我所知,您的脚本不会像inotifywait那样看待更改。还有一个小错误“第24行:查找匹配项时出现意外的EOF”()”
。什么是存储磁盘使用率(删除名称)?抱歉,丢失的)
是意外退格的结果。我还在答案中添加了一种使用数组来避免eval
的方法。我刚刚意识到您使用的不是/usr/bin/watch
,而是一个名为watch
的脚本,它可以改变事情。可能是一个显式的子shell<代码>/手表。bash-c'g++app.cpp-o app&&time./app&&du app'
watch g++ app.cpp -o app '&&' time ./app '&&' du app