nixCraft Linux Forum

nixCraft

Linux Tech Support Forum

changing dir

This is a discussion on changing dir within the Shell scripting forums, part of the Development/Scripting category; I am a linux noob migrated from windows anout a month ago and am lovng my fedora 5 set up. ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Register FAQ Members List Calendar Forgotten your password? Mark Forums Read
  #1 (permalink)  
Old 03-28-2006, 06:11 PM
Member
User
 
Join Date: Mar 2006
Posts: 35
Rep Power: 0
sparky
Default changing dir

I am a linux noob migrated from windows anout a month ago and am lovng my fedora 5 set up.
Any ways i look after a few linux servers some of which are gaming servers i decided to have a go at writing a script to start and stop servers which works fine, a menu for ip port max players etc then it starts the server (very pleased with my self )
i then decided to try and take it a step further when i log in via ssh i want excute myscript straight away with out navagting to each server i have a menu options ask me which server i would like to manage then i need it to cd to dir of my chioce but i can not seem to figure out how to make cd command work.
Code:
if [ $choice ==0 ] ; then CANT FIGURE OUT /home/cssource
elif [ $choice ==1] ; then cd /home/server01
etc
etc
Thanks for any help.
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-28-2006, 06:42 PM
tom tom is offline
Contributors
User
 
Join Date: Jun 2005
Location: London, UK
Posts: 213
Rep Power: 4
tom is on a distinguished road
Default

You are using == operator which is for string comparison and not for number comparison. Use –eq for equal to. In short,

Code:
if [ $choice -eq 0 ]; then
   cd /home/0
elif [ $choice -eq 1 ]; then
     cd /home/1
else
      echo "Etc etc"
fi
Reply With Quote
  #3 (permalink)  
Old 03-28-2006, 07:05 PM
Member
User
 
Join Date: Mar 2006
Posts: 35
Rep Power: 0
sparky
Default

Thanks for such a quick reply i tried that and no luck may be i am missing something here as i said i am a complete noob
I ll post my script
Code:
echo css server management--Please choose a server.
echo cssource [0]
echo cssource [1]
echo cssource [2]
echo cssource [3]
echo cssource [4]
echo cssource [5]
echo cssource [6]
echo cssource [7]
echo cssource [8]
echo cssource [9]
echo cssource [10]
echo cssource [11]
echo cssource [12]
#store vars
read choice
if [ $choice -eq 0 ] ; then cd 
	/home/sparky/backup
elif [ $choice -eq 1] ; then cd /home/server01
elif [ $choice -eq 2] ; then cd /home/server02
elif [ $choice -eq 3] ; then cd /home/server03
elif [ $choice -eq 4] ; then cd /home/server04
elif [ $choice -eq 5] ; then cd /home/server05
elif [ $choice -eq 6] ; then cd /home/server06 
elif [ $choice -eq 7] ; then cd /home/server07
elif [ $choice -eq 8] ; then cd /home/server08
elif [ $choice -eq 9] ; then cd /home/server09
elif [ $choice -eq 10] ; then cd /home/server10
elif [ $choice -eq 11] ; then cd /home/server11
elif [ $choice -eq 12] ; then cd /home/server12
else exit
fi
#server options.
echo please fill in these 3 questions and ill start your server


#Enter port 
echo -n Please enter port you wish to use :
read port

#tick
echo -n Please enter server tick rate :
read tick

#Max players
echo -n Please enter max players :
read players

./srcds_run -game cstrike -port $port +ip194.105.134.151 +maxplayers $players -tickrate $tick +map de_dust +exec server.cfg -autoupdate &
i then get this error
./css: line 28: /home/sparky/backup: is a directory
Thanks
Reply With Quote
  #4 (permalink)  
Old 03-28-2006, 07:22 PM
tom tom is offline
Contributors
User
 
Join Date: Jun 2005
Location: London, UK
Posts: 213
Rep Power: 4
tom is on a distinguished road
Default

Hmm

What is output of command:
Code:
ls -ld /home/sparky/backup
If directory does not exists; Create directory /home/sparky/backup:
Code:
mkdir -p /home/sparky/backup
Rest of script is okay i'm able to run it here
Reply With Quote
  #5 (permalink)  
Old 03-28-2006, 07:53 PM
Member
User
 
Join Date: Mar 2006
Posts: 35
Rep Power: 0
sparky
Default

All the dirs exist just can t get script to swap to them
out put of ls
Code:
[sparky@localhost ~]$ ls -ld /home/sparky/backup
drwxrwxr-x 2 sparky sparky 4096 Mar 27 20:55 /home/sparky/backup
when i thought to do the script i assumed the changing dir would be the easy part and the rest would be harder how wrong i was.
Reply With Quote
  #6 (permalink)  
Old 03-28-2006, 08:45 PM
monk's Avatar
Senior Member
User
 
Join Date: Jan 2005
Location: Tibet
My distro: Debian GNU/Linux
Posts: 482
Rep Power: 5
monk will become famous soon enough monk will become famous soon enough
Default

Replace
Code:
if [ $choice -eq 0 ] ; then cd
   /home/sparky/backup
With one line:
Code:
if [ $choice -eq 0 ] ; then cd /home/sparky/backup
If you type command /home/sparky/backup at a shell prompt you will always get error:
bash: /home/sparky/backup: is a directory

Same with shell script so just put it on one line.
Reply With Quote
  #7 (permalink)  
Old 03-28-2006, 09:17 PM
Member
User
 
Join Date: Mar 2006
Posts: 35
Rep Power: 0
sparky
Default

Thanks for all the speedy help so far getting there now it will change if i chosse option 0 but if i choose 1 to 12 i get
the code is as above but with the first if all on one line now
Code:
css server management--Please choose a server.
cssource [0]
cssource [1]
cssource [2]
cssource [3]
cssource [4]
cssource [5]
cssource [6]
cssource [7]
cssource [8]
cssource [9]
cssource [10]
cssource [11]
cssource [12]
12
./css: line 28: [: missing `]'
./css: line 29: [: missing `]'
./css: line 30: [: missing `]'
./css: line 31: [: missing `]'
./css: line 32: [: missing `]'
./css: line 33: [: missing `]'
./css: line 34: [: missing `]'
./css: line 35: [: missing `]'
./css: line 36: [: missing `]'
./css: line 37: [: missing `]'
./css: line 38: [: missing `]'
./css: line 39: [: missing `]'
I no i could just put the last part of the script in each dir and exec from there but it become a mission now
Reply With Quote
  #8 (permalink)  
Old 03-28-2006, 10:02 PM
rockdalinux's Avatar
Contributors
User
 
Join Date: May 2005
Location: Bangalore
My distro: RHEL, HP-UX, Solaris, FreeBSD, Ubuntu
Posts: 581
Rep Power: 7
rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough
Default

Ya here you go; following code should work...

Code:
echo css server management--Please choose a server.
echo cssource [0]
echo cssource [1]
echo cssource [2]
echo cssource [3]
echo cssource [4]
echo cssource [5]
echo cssource [6]
echo cssource [7]
echo cssource [8]
echo cssource [9]
echo cssource [10]
echo cssource [11]
echo cssource [12]
#store vars
read choice
if [ $choice -eq 0 ] ; then cd  /home/sparky/backup
elif [ $choice -eq 1 ] ; then cd /home/server01
elif [ $choice -eq 2 ] ; then cd /home/server02
elif [ $choice -eq 3 ] ; then cd /home/server03
elif [ $choice -eq 4 ] ; then cd /home/server04
elif [ $choice -eq 5 ] ; then cd /home/server05
elif [ $choice -eq 6 ] ; then cd /home/server06
elif [ $choice -eq 7 ] ; then cd /home/server07
elif [ $choice -eq 8 ] ; then cd /home/server08
elif [ $choice -eq 9 ] ; then cd /home/server09
elif [ $choice -eq 10 ] ; then cd /home/server10
elif [ $choice -eq 11 ] ; then cd /home/server11
elif [ $choice -eq 12 ] ; then cd /home/server12
else exit
fi
#server options.
echo please fill in these 3 questions and ill start your server


#Enter port
echo -n Please enter port you wish to use :
read port

#tick
echo -n Please enter server tick rate :
read tick

#Max players
echo -n Please enter max players :
read players

./srcds_run -game cstrike -port $port +ip194.105.134.151 +maxplayers $players -tickrate $tick +map de_dust +exec server.cfg -autoupdate &
And what was wrong ... Line

Code:
elif [ $choice -eq 1] ; then cd /home/server01
Should be
Code:
elif [ $choice -eq 1 ] ; then cd /home/server01
There should be space between 1 and ] so i just added a blank space for rest of all elif statements... just use above (copy and paste)
__________________
Rocky Jr.
You may have my body & soul, but you will never touch my pride!

If you have knowledge, let others light their candles at it.

Certified to work on HP-UX / Sun Solaris / RedHat
Reply With Quote
  #9 (permalink)  
Old 03-30-2006, 04:14 AM
Member
User
 
Join Date: Mar 2006
Posts: 35
Rep Power: 0
sparky
Default

That worked a treat thanks very much guys for all your help
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads

Thread Thread Starter Forum Replies Last Post
changing to SU in cronjob xpy Shell scripting 0 04-16-2008 02:29 PM
Bonding mode not changing bdptcob Linux software 3 06-05-2007 11:29 AM
Changing password at first login. Linux sbhupathireddy Linux software 1 07-05-2006 01:29 PM
changing desktop to kde All about FreeBSD/OpenBSD/NetBSD 1 01-11-2006 11:47 PM
linux changing run level Linux software 1 01-10-2006 07:25 PM


All times are GMT +5.5. The time now is 05:38 PM.


Powered by vBulletin® Version 3.7.4 - Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36