Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
help with scripting bash
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo
View previous topic :: View next topic  
Author Message
ilikeit
n00b
n00b


Joined: 06 Jun 2004
Posts: 28
Location: France (07)

PostPosted: Mon Oct 18, 2004 3:08 pm    Post subject: help with scripting bash Reply with quote

hi i need help scripting my first script (an HLDS management script).
my problem is to put the pid of hlds in a variable. i used this command but i think there is a problem with affectation
Code:
HLDS_PID=ps aux|grep hlds_r|grep -v grep|gawk '{ print $2 }'

returns error: aux: command not found.
i tried 'ps aux'|.. and "ps aux"|... but it dont seems to work
here is the full code:
Code:
case $1 in
start)
   HLDS_PID=ps aux|grep hlds_r|grep -v grep|gawk '{ print $2 }'
   if [ $HLDS_PID ]
   then
      echo "Server Already running on PID: $HLDS_PID"
   else
      cd /home/hlds/hlds_l
      ./hlds_run -game cstrike -secure -port 27015 +maxplayers 16 +map de_dust2 &
      echo "Server Started"
   fi
   ;;

stop)
   HLP=ps aux|grep hlds_r|grep -v grep|gawk '{ print $2 }'
   kill -9 $HLP
   echo "Server Stopped"
   echo $HLP
   ;;

stats)
   echo "PROC %MEM   %CPU"
   ps aux|grep hlds_|grep -v grep|gawk '{ print $2":"$3"    "$4" by "$1}'
   ;;

update)
   cd /home/hlds/hlds_l
   ./steam -command update -game cstrike -username ktaserv -password k76p455 remember_password
   echo "Server updated"
   ;;

*)
   echo "usage: $0 { start | stop | update | stats | clean }"
   echo " "
   echo " start  : démarre le serveur"
   echo " stop   : tue le serveur"
   echo " update : met le serveur a jour"
   echo " stats  : affiche les ressources "
   echo " "
   echo " script HLDS par kTa "
   ;;
esac
Back to top
View user's profile Send private message
dashnu
l33t
l33t


Joined: 21 Jul 2004
Posts: 703
Location: Casco Maine

PostPosted: Mon Oct 18, 2004 3:19 pm    Post subject: Reply with quote

I am no scritping expert but a shot in the dark for ya. I think that you want to set the env variable HLDS_PID at the top of your script and not inside of your case statment. Im most likely wrong but worth a try..


**edit ps -aux may help too .
_________________
write quit bang
Back to top
View user's profile Send private message
Wedge_
Advocate
Advocate


Joined: 08 Aug 2002
Posts: 3614
Location: Scotland

PostPosted: Mon Oct 18, 2004 3:23 pm    Post subject: Reply with quote

Try placing the command in backquotes like this:
Code:
HLDS_PID=`ps aux|grep hlds_r|grep -v grep|gawk '{ print $2 }'`

_________________
Per Ardua Ad Astra
The Earth is the cradle of the mind, but we cannot live forever in a cradle - Konstantin E. Tsiolkovsky
Gentoo Radeon FAQ
Back to top
View user's profile Send private message
Nate_S
Guru
Guru


Joined: 18 Mar 2004
Posts: 414

PostPosted: Mon Oct 18, 2004 10:46 pm    Post subject: Reply with quote

I too was going to say backquotes, but I was beaten to it.

backquotes execute the command inside them, and give the output to be used in a command.

for example

uname -r

2.6.9-rc1-nitro4

ls /lib/modules/`uname -r`

build
kernel
[snip]

that's the same thing as ls /lib/modules/2.6.9-rc1-nitro4, but the same command will still work when I change kernels (will output 2.6.9-rc3-nitro1 or whatever)

-Nate
Back to top
View user's profile Send private message
ilikeit
n00b
n00b


Joined: 06 Jun 2004
Posts: 28
Location: France (07)

PostPosted: Tue Oct 19, 2004 8:42 am    Post subject: Reply with quote

okay thanks thats really what i was looking for!

when to use #!/bin/sh and #!/bin/bash ? i only find differences when using it but not scripiting (for know as far as it works for sh i leave /bin/sh).

thank u mates,cya
ps: for a beginner and with a small enough font, its really hard to make difference between ' and `
Back to top
View user's profile Send private message
Nate_S
Guru
Guru


Joined: 18 Mar 2004
Posts: 414

PostPosted: Tue Oct 19, 2004 5:58 pm    Post subject: Reply with quote

It has to do with which shell you use to execute the script.

I'd leave it as /bin/bash, as that is likely your default shell, unless you have some reason for it to be /bin/sh.

-Nate
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum