Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a bash script that launches a ps command with a list of commands like this:

comando="ps -o pid,pcpu,cmd -C \"$2\""

$2 must be a variable in this form: command1 or command1 command2 command3 ... commandn

How can i check this variable in order to avoid errors? I need a control (with an if) to check the pattern.

EDIT: ps can be called in this way:

ps -o pid,pcpu,cmd -C "command1 command2 ... commandn". If someone calls this bash script as script.sh -C " command1 command2", the ps command returns "IMPROPER LIST". I want to avoid the ps error and echo my error before launching ps command.

share|improve this question
    
Is this homework? –  Sven Aug 13 '10 at 9:20
    
Yeah! Do this change something? :P –  Marco de Nadai Aug 13 '10 at 10:44

2 Answers 2

up vote 1 down vote accepted

You should avoid putting commands in variables.

If spaces are what are tripping you up, you can strip them like this:

ps -o pid,pcpu,cmd -C ${2// }

That will strip all spaces though, so if any of your command strings contain spaces it will ruin them. This will strip leading and trailing spaces and spaces on either side of commas:

commands=${2/# }          # leading space
commands=${command/% }    # trailing space
commands=${command/, /,}  # spaces after commas
commands=${command/ ,/,}  # spaces before commas
ps -o pid,pcpu,cmd -C "$commands"

If you want to return an error instead of sanitizing and running the input, just do that space removal or other sanitizing and check to see if the result is equal to the input. If they're not then output an error.

share|improve this answer
    
perfect, i can sanitize the command without giving any error! Thank you –  Marco de Nadai Aug 13 '10 at 15:41

It could work with a for loop

arguments="command1 command2 command3 command4 command5"
for arg in $arguments
do
    ps -o pid,pcpu,cmd -C $arg
done

results:

ps -o pid,pcpu,cmd -C command1
ps -o pid,pcpu,cmd -C command2
ps -o pid,pcpu,cmd -C command3
ps -o pid,pcpu,cmd -C command4
ps -o pid,pcpu,cmd -C command5

Perhaps could you give a better real example of how it should work what are you expecting ... it doesnt look too clear to me.


To call it from the command line the way i explainned in the comment just use $* to catch all the arguments passed.

for arg in $*
do
    ps -o pid,pcpu,cmd -C $arg
done

then call it like this:

./test.sh command1 command2 command3 command4 command5 command6

and it will run the ps command with each argument.

share|improve this answer
    
PS can be called in this way: ps -o pid,pcpu,cmd -C "command1 command2 ... commandn". If someone call this bash script as script.sh -C " command1 command2", the ps command returns "IMPROPER LIST". I want to avoid the PS error and echo a my error before lunching ps command. Is it clear? :D –  Marco de Nadai Aug 13 '10 at 10:43
    
so what you really want is to be able to parse arguments to your test.sh that will be used on the -C option of your ps command, for example: test.sh command1 command2 command3 and they will run as the above output is that correct ? –  Prix Aug 13 '10 at 20:42
    
see the answer for updates of the above comment. –  Prix Aug 13 '10 at 20:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.