views:

412

answers:

1

Hi, I'm trying to make a program that takes in one argument. A file. And then checks what has happened to the file 60 seconds later. To do this, I need to store the result from "-e $1" in a variable and then check it 60 seconds later. I can not seem to be able to make the if expression listen to me, and I know it's wrong. For testing purposes this script just prints out immediately the result of the comparison. Looking forward to a working sample of this, I don't know how many versions I've made of this tiny program. Thanks! It's due tomorrow, any help is very much appreciated!

#!/bin/bash
onStartup=$(test -e $1) 
if [ -e "$1" ]; then
unixtid1=$(date  +"%s" -r "$1") #To check if the file was edited. 
echo $unixtid1
fi
sleep 3

#Here trying to be able to compare the boolean value stored in the
#start of the script. True/False or 1 or 0? Now, both is actually printed. 
if [[ $onStartup=1 ]]; then
echo "Exists"
fi

if [[ $onStartup=0 ]]; then
echo "Does not exists"
fi
+2  A: 

Use $? special shell variable to get the result of the command. Remember that return value of 0 means true. Here is modified script

#!/bin/bash
test -e $1
onStartup=$?

if [ $onStartup -eq 0 ]; then
unixtid1=$(date  +"%s" -r "$1") #To check if the file was edited. 
echo $unixtid1
fi
sleep 3

#Here trying to be able to compare the boolean value stored in the
#start of the script. True/False or 1 or 0?
if [[ $onStartup -eq 0 ]]; then
echo "Exists"
else
echo "Does not exists"
fi

Your original example tried to store a literal output of the test command in the onStartup variable. The literal output of the test command is an empty string, that's why you did not see any output.

Alexander Pogrebnyak