LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 04-26-2011, 02:25 AM   #1
m3phisto
Member
 
Registered: Mar 2010
Posts: 47

Rep: Reputation: 15
small bash script


hi there, i've written a small cript to blank a file. but there seems to be a mistake in line 33.
can someone help me with this?



Code:
#!/bin/bash



 errormail()
 {
        for host in ${mailaddr[@]}; do
                /bin/mail -s "$subject" $host < "$cmd"
        done
echo mail...
 }


node=`/bin/hostname -s`
subject="An Error occured in delAutoresponder.sh on $node"



mailaddr=("xxx.xxx@xxx.at")
subject="An ERROR occured in delAutoresponder.sh"


#file="/var/spool/mail/autoresponder"
file="/gfsdisk/scripts/test"


if [ -f $file ]
  then

    cmd=`true > $file`
    echo $?
    if [$? ne 0]
      then
         errormail
    fi
fi

thx a lot

Last edited by m3phisto; 04-26-2011 at 02:44 AM.
 
Old 04-26-2011, 02:38 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Code:
line 33: if [$? ne 0]
should be
Code:
line 33: if [ $? -ne 0 ]
that is you need spaces to separate the expression from the opening and closing brackets. Moreover the integer comparison operators always begin with a hyphen. Just take in mind that [ is both a bash built-in and a command. You can verify this assertion on your system by means of
Code:
type -a [
This means that the syntax common to all the linux command is applied, that is <command name><space><argument><space><argument>... and so on. This explains the mandatory presence of the spaces. Instead, the closing bracket ] is the mandatory last command argument.
 
Old 04-26-2011, 02:39 AM   #3
dudeman41465
Member
 
Registered: Jun 2005
Location: Kentucky
Distribution: Ubuntu
Posts: 794

Rep: Reputation: 56
Nevermind, somebody beat me to it,
 
Old 04-26-2011, 02:45 AM   #4
m3phisto
Member
 
Registered: Mar 2010
Posts: 47

Original Poster
Rep: Reputation: 15
thx for your help but i still get the massage

line 33: [0: command not found
 
Old 04-26-2011, 03:03 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Quote:
Originally Posted by m3phisto View Post
thx for your help but i still get the massage

line 33: [0: command not found
This still means the same: there is no space between the [ and $?. It should be
Code:
if<space>[<space>$?<space>-ne<space>0<space>]
 
1 members found this post helpful.
Old 04-26-2011, 03:04 AM   #6
m3phisto
Member
 
Registered: Mar 2010
Posts: 47

Original Poster
Rep: Reputation: 15
thx a lot! it works now.
 
Old 04-26-2011, 03:17 AM   #7
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Rep: Reputation: 12
please mark your thread as solved

 
Old 04-26-2011, 04:38 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,508

Rep: Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890
You could also combine your tests too:
Code:
if [[ -f $file ]] && ! : > $file
then
    errormail
fi
 
  


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
Small part of Bash Script that I would like to explain it to me jefn Programming 10 04-11-2009 04:09 AM
Very small bash script enemorales Programming 8 02-25-2005 02:47 AM
small bash script issue zoomzoom Linux - General 7 06-08-2004 06:33 PM
small bash script to kill a PID flosch Linux - General 3 05-06-2004 09:28 PM
sed in small BASH script OhLordy Linux - General 1 08-29-2003 11:32 AM


All times are GMT -5. The time now is 11:53 AM.

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