Web Hosting Talk







View Full Version : Basic Bash script help


MattF
04-14-2004, 01:04 PM
I'm having a little trouble writting a simple bash script to replace cPanel's unreliable cpuwatch.

cpuwatch takes 2 arguments max_load and command.

e..g
./cpuwatch 2.5 /bin/ls

I want a simple to create a simple pass-through bash script to just execute the command ($2 I believe) and ignore the load figure ($1). Any help?

dan_erat
04-14-2004, 01:44 PM
Uh...
#!/bin/sh
$2
Is that all you needed, or did I completely misunderstand your question?

MattF
04-14-2004, 02:40 PM
Sorry I forgot the command could have between 0 and x number of arguments. Just using $2 would discard the arguments.

rghf
04-14-2004, 02:53 PM
Isn't it shift to move the variables over?

Rus

dan_erat
04-14-2004, 02:55 PM
Good call. I think that "shift; $@" does the trick.

MattF
04-14-2004, 03:02 PM
#!/bin/sh

shift 1;

$*


What's the difference between $@ and $*

dan_erat
04-14-2004, 04:57 PM
(after looking it up in a book):

$@ evaluates to separate double-quoted strings for each parameter, while $* evaluates to a single string containing all of the parameters separated by the first character of $IFS.

To see the difference:
#!/bin/sh
IFS=,
echo "$@"
$ ./blah.sh a b c d e
a b c d e
#!/bin/sh
IFS=,
echo "$*"
$ ./blah.sh a b c d e
a,b,c,d,e

Bashar
04-14-2004, 07:47 PM
what does IFS stand for?

dan_erat
04-14-2004, 07:52 PM
What do I look like, a reference librarian? :)

Internal Field Separator though.

Bashar
04-14-2004, 09:14 PM
easy on me :P

i thought its a variable but it was doing its job without being recalled, thats what confused me :)