Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

drenriza

macrumors newbie
Original poster
Jan 24, 2011
27
0
I have created a bash script and in crontab -e created an entry called
*/10 * * * * /command/
But when i try to execute the script from the crontab, it grabs a wrong value.
it runs
test=$(ifconfig en0 |grep -o 10.1.42)
where i have made 10.1.42 = true / it exists
when it then gets to an if then statement it takes the else, instead of the then.

if [ "$test" == "10.1.42" ]
then
(echo "Connection to the fileserver -> Level3 folder established" >> /Users/$hvem/startUpScripts/log)
/Users/$hvem/startUpScripts/Level3ExpectScript
else
(echo "Your not on the correct network 10.1.42.* didn't mount ${tid}" >> /Users/$hvem/startUpScripts/log)
fi

BUT if i run the script by "hand" ./scriptName it gets the corrects value and connects to a remote samba server i got, by the then instead of the else statement.

Whats going on with this script? Why docent it work correctly just because i execute it from the crontab?

Kind regards.
 
Not on a *nix machine but shouldn't test=$(ifconfig en0 |grep -o 10.1.42) make test equal to the full IP?
 
Not on a *nix machine but shouldn't test=$(ifconfig en0 |grep -o 10.1.42) make test equal to the full IP?

The idea was to make a script that automatically mapped a samba share i got at my office. And to do so i only need the 3 first octets of the ip address, to make sure that we are on the corporate network.

But for some strange reason the scripts behaves differently between executing it from ./scriptName and crontab. And i'm just wondering why it behaves differently. Do you know this?
 
Nope, but I'd think it shouldn't work when run normally because [ "$test" == "10.1.42" ] is basically [ "10.1.42.xxx" == "10.1.42" ] and should be [ "$test" == "10.1.42*" ], no ?
 
Cron usually runs without an environment (and therefore without the PATH environment variable), so you might want to specify the complete paths to ifconfig and grep:

james@silverbox ~ $ which ifconfig
/sbin/ifconfig
james@silverbox ~ $ which grep
/usr/bin/grep
 
how can it be that
if i execute the command by hand /bin/date |/usr/bin/awk '{print $5}' i get the time xx:xx:xx
if i execute the exact same line in crontab i get the output CEST

???
 
Last edited:
how can it be that
if i execute the command by hand /bin/date |/usr/bin/awk '{print $5}' i get the time xx:xx:xx
if i execute the exact same line in crontab i get the output CEST

???

What does date and awk have to do with your script above? date might return something different based on your environment variables as well, don't really know. I do know cron though and 99% of the problems like what you are reporting is because the environment variables are non-existent in a cron process. Did you try the changes I recommended?

test=$(/sbin/ifconfig en0 | /usr/bin/grep -o 10.1.42)
 
It now reaches the correct if statement. But it docent execute my expect script located at /Users/$hvem/startUpScripts/Level3ExpectScript it only run the echo before the expect script.

Also i find it strange that when i run the date command
tid=$(/bin/date |/usr/bin/awk '{print $4}')

i need to print the 4th column instead of the 6th to get the time.
Anyone who can tell me the reason for this?

Kind regards
 
Last edited:
As I said before, date probably returns something different because of an environment variable, probably the time zone (TZ). What value of date output specifically are you looking for? You could just use the -f flag to date and get just the value you need without using awk.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.