LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   My first shell script (https://www.linuxquestions.org/questions/linux-newbie-8/my-first-shell-script-159310/)

asktoby 03-18-2004 04:20 AM

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"?

Demonbane 03-18-2004 05:06 AM

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


asktoby 03-18-2004 05:44 AM

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 :)

Demonbane 03-18-2004 06:09 AM

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


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