LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-18-2004, 05:20 AM   #1
asktoby
Member
 
Registered: Jul 2003
Location: Cardiff, England ;)
Distribution: Mandrake 9.1
Posts: 94

Rep: Reputation: 15
My first shell script


Hi

I wrote my first shell script yesterday. It seems to work well, but I thought I'd post it up in case someone could suggest "It would work better if you..."

I wanted my teamspeak server to always be running on my PC.

I've put this short script in my ~/.bash_profile file to check if the server is already running, and if it is not, to start it.

I find it useful because I sometimes reboot linux, and often log into linux remotely. If the server was started without checking if it was already running, I would get various issues.

This is my first attempt at shell scripting, it seems to work well, but I'd appreciate any comments!


Code:
cat ~/Documents/downloads/teamspeak/tss2_rc2/tsserver2.pid
if test $? -eq 1
then
 cd ~/Documents/downloads/teamspeak/tss2_rc2
 ./server_linux -PID=tsserver2.pid
 echo tsserver2.pid not found, tsserver started!
 else
 echo tsserver.pid found. tsserver is assumed to be running.
fi
cd
Obviously, replace ~/Documents/downloads... with the path to your own teamspeak install directory.

One thing that I'd like to do to improve it is in the case of tsserver.pid not existing,
cat ~/Documents/downloads/teamspeak/tss2_rc2/tsserver2.pid
outputs
cat: /home/toby/Documents/downloads/teamspeak/tss2_rc2/tsserver2.pid: No such file or directory
It would be nice if this was silent. I've tried adding "> /dev/null" to no avail. Any suggestions? Something like "echo off"?
 
Old 03-18-2004, 06:06 AM   #2
Demonbane
LQ Guru
 
Registered: Aug 2003
Location: Sydney, Australia
Distribution: Gentoo
Posts: 1,796

Rep: Reputation: 47
Something like this should work too:
Code:
#!/bin/bash
                                                                                
cd ~/Documents/downloads/teamspeak/tss2_rc2
                                                                                
if [ -f tsserver2.pid ]; then

    ./server_linux -PID=tsserver2.pid
    echo "tsserver2.pid not found, tsserver started!"

else

    echo "tsserver.pid found. tsserver is assumed to be running."

fi

Last edited by Demonbane; 03-18-2004 at 06:08 AM.
 
Old 03-18-2004, 06:44 AM   #3
asktoby
Member
 
Registered: Jul 2003
Location: Cardiff, England ;)
Distribution: Mandrake 9.1
Posts: 94

Original Poster
Rep: Reputation: 15
Toby goes and looks up:
if [ -f filename]; then

Oh, yes, that's much tidier! And there was me trying to work out how to get "cat" to supress error messages. (Strangely, I could find details on the web of cat -s and cat -q switches, but these weren't working at all.)


The if/else is the wrong way around in your suggestion though. Also, I'm not putting #!/bin/bash at the beginning because this snippet is just pasted onto the end of my already existing ~/.bash_profile.

So, the (final?) polished up version would be:

Code:
# == Teamspeak autostarter script =====
if [ -f ~/Documents/downloads/teamspeak/tss2_rc2/tsserver2.pid ]; then
        echo "tsserver.pid found. Teamspeak is assumed to be running."
else
        cd ~/Documents/downloads/teamspeak/tss2_rc2
        ./server_linux -PID=tsserver2.pid
        cd
fi
# == End of Teamspeak script ==========
Thanks for the tips
 
Old 03-18-2004, 07:09 AM   #4
Demonbane
LQ Guru
 
Registered: Aug 2003
Location: Sydney, Australia
Distribution: Gentoo
Posts: 1,796

Rep: Reputation: 47
by the way if you really want to supress the error message, you redirect standard error
Code:
cat nonexistant-file 2> /dev/null
or get rid of any output completely:
Code:
cat nonexistant-file 1>/dev/null 2>&1
check out the bash programming intro in http://www.tldp.org

Last edited by Demonbane; 03-18-2004 at 07:10 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script inside shell script treotan Linux - General 4 02-19-2009 07:34 AM
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 03:14 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 06:32 PM
Directory listing - Calling shell script from a CGI script seran Programming 6 08-12-2005 12:08 AM
[SHELL SCRIPT] Write at the right of the shell window Creak Linux - General 2 04-02-2004 04:00 PM


All times are GMT -5. The time now is 09:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration