Andrew Bold
Andrew Bold

Reputation:

Verbose output of shell script

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

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:

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!

Upvotes: 33

Views: 120208

Answers (4)

Brian Agnew
Brian Agnew

Reputation: 272307

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

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/bash invocation in your script.

Upvotes: 74

Steve B.
Steve B.

Reputation: 57324

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

Upvotes: 0

Bert F
Bert F

Reputation: 87543

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

::::::::::::::
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

Upvotes: 1

seb
seb

Reputation: 1608

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

Upvotes: 3

Related Questions