Linux shell 脚本的详细输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/956213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:24:03  来源:igfitidea点击:

Verbose output of shell script

linuxshell

提问by

I have a very simple shell script that looks as follows:

我有一个非常简单的 shell 脚本,如下所示:

clear

for i in -20 -19 -18 -17 -16 -15 -14 ... 18 19  
do  
echo "Nice value is $i"  
nice -n $i ./app1   
done  

Basically, I wanna run an application with all different priority values between -20 and 19. However, when executing this script it looks as follows:

基本上,我想运行一个具有 -20 和 19 之间所有不同优先级值的应用程序。但是,在执行此脚本时,它看起来如下:

Nice value is -20  
15916233  
Nice value is -19  
5782142  
....  
Nice value is 19  
5731287  

But I would like some kind of verbose output, that is also printing the command on the terminal so that it looks like this

但我想要某种详细的输出,也就是在终端上打印命令,使其看起来像这样

Nice value is -20  
nice -n -20 ./app1    
15916233  
Nice value is -19  
nice -n -19 ./app1   
5782142  
....  
Nice value is 19  
nice -n 19 ./app1   
5731287  

Is there a way to do that? Thank you!

有没有办法做到这一点?谢谢!

回答by Brian Agnew

You don't say what sort of shell you're running. If you're using sh/bash, try

你没有说你正在运行什么样的shell。如果您使用的是 sh/bash,请尝试

sh -x script_name

sh -x script_name

to run your script in a verbose/debug mode. This will dump out all the commands you execute, variable values etc. You don't want to do this normally since it'll provide a ton of output, but it's useful to work out what's going on.

在详细/调试模式下运行您的脚本。这将转储您执行的所有命令、变量值等。您通常不想这样做,因为它会提供大量输出,但了解正在发生的事情很有用。

As noted in the comments, you can add this flag to your #!/bin/bashinvocation inyour script.

如评论中所述,您可以将此标志添加到脚本中的#!/bin/bash调用

回答by seb

an easy way:

一个简单的方法:

for i in -20 -19 -18 -17 -16 -15 -14 ... 18 19
do
  echo "Nice value is $i"
  echo "nice -n $i ./app1"
  nice -n $i ./app1
done

回答by Steve B.

let I=-20
while [ $I -lt 20 ]; do
  echo "Nice value is $I"
  nice -n $I ./app1
  let I=$I+1
done

回答by Bert F

These will demonstrate 'eval' and 'set' to do what you want:

这些将演示 'eval' 和 'set' 来做你想做的事:

::::::::::::::
a.sh
::::::::::::::
#!/bin/sh

clear

i=-20
while [ ${i} -lt 20 ]; do
  echo "Nice value is $i"
  cmd="nice -n $i ./app1"
  echo ${cmd}
  eval ${cmd}
  i=`expr ${i} + 1`
done

::::::::::::::
b.sh
::::::::::::::
#!/bin/sh

clear

i=-20
while [ ${i} -lt 20 ]; do
  echo "Nice value is $i"
  set -x
  nice -n $i ./app1
  set +x
  i=`expr ${i} + 1`
done