LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-07-2007, 07:07 PM   #1
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
bash scripting testing for file


I am trying to make a boot script for my wireless card damon that sometimes when shutting the computer down properly doesn't remove the /var/run/damon file. Ive been reading some bash stuff but its getting rather confusing. Below is what I think you can do but im not sure.


Code:
 
if [ cat /var/run/damon ] 
then rm /var/run/damon
 
Old 08-07-2007, 07:28 PM   #2
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
Quote:
if [ cat /var/run/damon ]
then rm /var/run/damon
yea, this is a confusing issue. the if operator in bash checks the return value of the command following it. the [ ] part is actually a command. now, i'm not an expert on the history here or anything, but basically the command is called test, and [ is an abbreviation of it. bash may have a build in [ ] operator by now, but at any rate, the functionality is the same.

for the case above to work, you would need to remove the []'s

Code:
if cat /var/run/damon
then
rm /var/run/damon
this relies on the cat command returning 0 if the file you are trying to cat exists. what you probably would rather do is

Code:
if [ -e /var/run/damon ]
then
rm /var/run/damon
which will actually do the same thing as

Code:
if test -e /var/run/damon
then
rm /var/run/damon
the test command, or [ for short, is checking if the file exists, and will return 0 if it does (which is the unix "there were no errors" return code)
 
Old 08-07-2007, 07:30 PM   #3
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
and for some reason i forget the fi on all the if statments

Code:
if [ -e /var/run/damon ]
then
rm /var/run/damon
fi
 
Old 08-07-2007, 07:51 PM   #4
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
Ahh ok the [] is a actually command not an enclosure bracket. That is where I was getting confused.

Thanks for the help this helps alot.


Now I just need to figure out why the deamon does not exit properly when I shut the system down.
 
Old 08-08-2007, 03:42 PM   #5
fvu
LQ Newbie
 
Registered: Nov 2006
Distribution: Ubuntu 7.10
Posts: 28

Rep: Reputation: 15
You can also use the `&&' operator to write a concise one-liner:
# If file exists, remove it
[ -e /var/run/damon ] && rm /var/run/damon
If you want to make sure code is executed on exit, you might try setting a bash trap, see template below.
For more information see: http://fvue.nl/wiki/Bash:_Error_handling

Code:
#!/bin/bash
# Example script for handling bash errors: Exit on error and trap exit.
# This script is supposed to run in a subshell.
# See also: http://fvue.nl/wiki/Bash:_Error_handling
    
set -o errtrace           # Let shell functions inherit ERR trap.  Same as `set -E'.
set -o nounset            # Trigger error when expanding unset variables.  Same as `set -u'.
trap onexit 1 2 3 15 ERR  # Trap non-normal exit signals: 1/HUP, 2/INT, 3/QUIT, 15/TERM, ERR

# Process `exit' event
# @param $1 integer  (optional) Exit status.  If not set, use `$?' (exit status previous command)
function onexit() {
    local exit_status=${1:-$?}
    # myscript here...
    echo Exiting $0 with $exit_status
    exit $exit_status
}

# myscript here...

onexit  # Allways call `onexit' at end of script
Hope this helps,
Freddy
http://fvue.nl/wiki/Bash

Last edited by fvu; 08-08-2007 at 04:38 PM.
 
  


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
Bash Scripting Empty file john8675309 Linux - Software 2 08-08-2007 04:05 PM
bash scripting - how to make config file? Neorio Programming 3 10-22-2004 06:39 AM
bash scripting read from file cadj Programming 2 02-29-2004 10:42 PM
bash scripting - how long ago file was modified brian0918 Programming 3 06-19-2003 04:10 PM
bash scripting - largest # file extension brian0918 Programming 3 06-16-2003 12:06 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:36 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