Script output:
-bash-4.1$ ./test.sh
+ '[' -f /tmp/test/ping_check.txt ']'
+ FILEDATE=0
+ echo 0
+ [[ '' -ne 0 ]]
Script:
-bash-4.1$ cat test.sh
#!/bin/bash
set -x
if [ -f /tmp/test/ping_check.txt ];
then
FILEDATE=`cat /tmp/test/ping_check.txt`
else
FILEDATE=0
fi
echo $FILEDATE
if [[ $NOWDATE -ne $FILEDATE ]]; then
if [[ $CMD_RET =~ ", 0% packet loss" ]]
then
echo "$HOST pinged ..."
echo $NOWDATE > /tmp/test/ping_check.txt
# run backup script
/tmp/test/backup-host-1
else
echo "$HOST did not ping ..."
fi
fi
You need double quotes to get rid of that error, as far as my test
results show.
SDA
Quoting canito at dalan.us:
> When writing bash I make sure to standardize when validating.
>
> I don't use double square brackets [[ unless using special
> validators that require this.
>
> I would recommend that you use the double brackets throughout your script.
>
> Also, when validating I take the extra step and use the following
> for example:
>
> if ${variable} -eq 0 ]; then
>
> Using the {} around the variable has a special meaning making use
> that only the value is expanded, and it works great.
>
> When troubleshooting scripts I comment our every block making sure
> the results are what I want. Then, I am able to move forth.
>
> Hope this helps.
>
> Saul David Alanis
>
> Quoting Raymond Norton <admin at lctn.org>:
>
>> Bash:
>>
>> #!/bin/bash -x
>>
>>
>>
>>
>> On 11/12/2013 10:50 AM, Jake Vath wrote:
>>> Are you trying to run this as a Bash script with /#!/bin/bash/ (or
>>> some from of Bash)?
>>> or are you trying to run this as a Bourne shell with /#!/bin/sh/
>>> (or some form of Borne)?
>>>
>>> -> Jake
>>>
>>>
>>> On Tue, Nov 12, 2013 at 9:01 AM, Raymond Norton <admin at lctn.org
>>> <mailto:admin at lctn.org>> wrote:
>>>
>>> I run the following script to test if a host is on-line and
>>> trigger a back up if it's alive. Something apparently changed with
>>> the script and it is giving me a "unary operator expected" error
>>> for line 17.
>>>
>>> Any ideas what is triggering it?
>>>
>>>
>>>
>>> if [ -f /etc/bacula/ping_check.txt ];
>>> then
>>> FILEDATE=`cat /etc/bacula/ping_check.txt`
>>> else
>>> FILEDATE=0
>>> fi
>>>
>>> if [ $NOWDATE -ne $FILEDATE ];
>>> then
>>> if [[ $CMD_RET =~ ", 0% packet loss" ]]
>>> then
>>> echo "$HOST pinged ..."
>>> echo $NOWDATE > /etc/bacula/ping_check.txt
>>> # run backup script
>>> /etc/bacula/backup-host-1
>>>
>>> else
>>> echo "$HOST did not ping ..."
>>> fi
>>> fi
>>>
>>> exit
>>>
>>> -- Raymond Norton
>>> LCTN
>>> 952.955.7766 <tel:952.955.7766>
>>>
>>> Sent from My Desktop
>>>
>>> _______________________________________________
>>> TCLUG Mailing List - Minneapolis/St. Paul, Minnesota
>>> tclug-list at mn-linux.org <mailto:tclug-list at mn-linux.org>
>>> http://mailman.mn-linux.org/mailman/listinfo/tclug-list
>>>
>>>
>>>
>>> --
>>> This message has been scanned for viruses and
>>> dangerous content by *MailScanner* <http://www.mailscanner.info/>, and is
>>> believed to be clean.
>>>
>>>
>>> _______________________________________________
>>> TCLUG Mailing List - Minneapolis/St. Paul, Minnesota
>>> tclug-list at mn-linux.org
>>> http://mailman.mn-linux.org/mailman/listinfo/tclug-list
>>
>> --
>> Raymond Norton
>> LCTN
>> 952.955.7766
>>
>> Sent from My Desktop
>
>
>
> _______________________________________________
> TCLUG Mailing List - Minneapolis/St. Paul, Minnesota
> tclug-list at mn-linux.org
> http://mailman.mn-linux.org/mailman/listinfo/tclug-list