bash script

Discussion in 'macOS' started by drenriza, Sep 29, 2011.

  1. drenriza macrumors newbie

    Joined:
    Jan 24, 2011
    #1
    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.
     
  2. Hansr macrumors 6502a

    Joined:
    Apr 1, 2007
  3. drenriza thread starter macrumors newbie

    Joined:
    Jan 24, 2011
    #3
    Sure that could be done and tweaked to work. But why docent it just work? If i can run it by hand, then why not by cronjob and get the same result?
     
  4. Hansr macrumors 6502a

    Joined:
    Apr 1, 2007
    #4
    Not on a *nix machine but shouldn't test=$(ifconfig en0 |grep -o 10.1.42) make test equal to the full IP?
     
  5. drenriza thread starter macrumors newbie

    Joined:
    Jan 24, 2011
    #5
    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?
     
  6. Hansr macrumors 6502a

    Joined:
    Apr 1, 2007
    #6
    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 ?
     
  7. jameslmoser macrumors 6502

    Joined:
    Sep 18, 2011
    Location:
    Las Vegas, NV
    #7
    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
     
  8. drenriza, Sep 30, 2011
    Last edited: Sep 30, 2011

    drenriza thread starter macrumors newbie

    Joined:
    Jan 24, 2011
    #8
    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

    ???
     
  9. jameslmoser macrumors 6502

    Joined:
    Sep 18, 2011
    Location:
    Las Vegas, NV
    #9
    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)
     
  10. drenriza, Oct 3, 2011
    Last edited: Oct 3, 2011

    drenriza thread starter macrumors newbie

    Joined:
    Jan 24, 2011
    #10
    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
     
  11. jameslmoser macrumors 6502

    Joined:
    Sep 18, 2011
    Location:
    Las Vegas, NV
    #11
    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.
     

Share This Page