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 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"? |
Something like this should work too:
Code:
#!/bin/bash |
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 ===== |
by the way if you really want to supress the error message, you redirect standard error
Code:
cat nonexistant-file 2> /dev/null Code:
cat nonexistant-file 1>/dev/null 2>&1 |
All times are GMT -5. The time now is 09:25 PM. |