tags:

views:

2196

answers:

5

Hi

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!

+4  A: 

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.

Brian Agnew
You should also be able to make this happen for every run by putting the -x in the #! line; i.e. the first line of the script would be: #!/bin/bash -x
PTBNL
that looks good and does exactly what I want. thanks!
+2  A: 

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
seb
A: 
let I=-20
while [ $I -lt 20 ]; do
  echo "Nice value is $I"
  nice -n $I ./app1
  let I=$I+1
done
Steve B.
I am using bash, when trying to run your code I get the following error: [: -lt: unary operator expected
This here seems to do the stuffclear for (( i = -20 ; i <= 19; i++ ))do echo "Welcome $i times" nice -n $i ./app1 done
sorry, mixed up the Upper and Lowercase I's (quick typing). They should all be the same of course. Edited to fix.
Steve B.
You also have to be careful with spaces in bash. For example, "while [$I -lt 10]" doesn't work.
Steve B.
A: 

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
Bert F
A: 

Something more succinct would be

CMD="nice -n \$i ./app1"
for i in `seq -19 19`
do
  echo $CMD
  eval $CMD
done
lsc
Also looks excellent. I like it with the seq -19 19. Is there also a way to specify the step, e.g. lets say step is 3 then i has just the values -19, -16, -13, etc?
you can specify the step size as a second argument, for example `seq -19 3 19`
lsc