a simple shell script / crontab question - Unix

This is a discussion on a simple shell script / crontab question - Unix ; I'm still pretty noobish, trying to get a shell script to do something. Can someone tell me why this is happening, or possibly a better way of doing it? Assume the file: ~/flag is a file whose contents are either ...

+ Reply to Thread
Results 1 to 7 of 7

Thread: a simple shell script / crontab question

  1. a simple shell script / crontab question

    I'm still pretty noobish, trying to get a shell script to do something.
    Can someone tell me why this is happening, or possibly a better way of
    doing it?

    Assume the file: ~/flag is a file whose contents are either a single
    digit 1 or 0. I want the script to read the file, and if it contains a
    1, I want make a copy of the file called flag2. Here's what I came up
    with:

    go=`cat ~/flag`
    if [ $go == 1 ]
    then
    cp ~/flag ~/flag2
    fi


    If I run this script from the shell prompt, it works exactly as
    expected. However if I set crontab to run it while I'm not logged in,
    it doesnt work and I come back to email from cron saying:

    [: 0: unexpected operator

    What's going on here? TIA.


  2. Re: a simple shell script / crontab question

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    yerk5@hotmail.com wrote:
    [snip]
    > go=`cat ~/flag`
    > if [ $go == 1 ]
    > then
    > cp ~/flag ~/flag2
    > fi
    >
    >
    > If I run this script from the shell prompt, it works exactly as
    > expected. However if I set crontab to run it while I'm not logged in,
    > it doesnt work and I come back to email from cron saying:
    >
    > [: 0: unexpected operator
    >
    > What's going on here? TIA.


    At a guess, cron uses a different shell than you do at the command prompt

    Some shells have the '[' command built in, and some don't
    The built-in version of '[' recognizes the == equality test
    The command version (an alias of the 'test' binary) does not

    When you run your script at the commandline, you probably get the builtin
    version of [, which recognizes == as valid

    When you run your script through cron, you probably get a different shell, which
    defaults to the command version of [, which in turn does not recognize == as valid

    Try prepending a hashbang to your script, pointing it at the proper shell
    interpreter. Something like...

    #!/bin/bash
    go=`cat ~/flag`
    if [ $go == 1 ]
    then
    cp ~/flag ~/flag2
    fi

    - --
    Lew Pitcher
    IT Specialist, Enterprise Data Systems,
    Enterprise Technology Solutions, TD Bank Financial Group

    (Opinions expressed are my own, not my employers')
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.4 (MingW32)

    iD8DBQFDeNn9agVFX4UWr64RApO1AKDE/JG9TDcc/cZW8BMRa50QRpPfgwCfdhzH
    zAFPpJRU1XqYJ8M/BkHpJz4=
    =gSJQ
    -----END PGP SIGNATURE-----

  3. Re: a simple shell script / crontab question

    Sweet. Worked like a charm. I suspected it was something along those
    lines, but you crystalized it for me. It's fun to learn. Thanks again!

    Lew Pitcher wrote:
    > -----BEGIN PGP SIGNED MESSAGE-----
    > Hash: SHA1
    >
    > yerk5@hotmail.com wrote:
    > [snip]
    > > go=`cat ~/flag`
    > > if [ $go == 1 ]
    > > then
    > > cp ~/flag ~/flag2
    > > fi
    > >
    > >
    > > If I run this script from the shell prompt, it works exactly as
    > > expected. However if I set crontab to run it while I'm not logged in,
    > > it doesnt work and I come back to email from cron saying:
    > >
    > > [: 0: unexpected operator
    > >
    > > What's going on here? TIA.

    >
    > At a guess, cron uses a different shell than you do at the command prompt
    >
    > Some shells have the '[' command built in, and some don't
    > The built-in version of '[' recognizes the == equality test
    > The command version (an alias of the 'test' binary) does not
    >
    > When you run your script at the commandline, you probably get the builtin
    > version of [, which recognizes == as valid
    >
    > When you run your script through cron, you probably get a different shell, which
    > defaults to the command version of [, which in turn does not recognize == as valid
    >
    > Try prepending a hashbang to your script, pointing it at the proper shell
    > interpreter. Something like...
    >
    > #!/bin/bash
    > go=`cat ~/flag`
    > if [ $go == 1 ]
    > then
    > cp ~/flag ~/flag2
    > fi
    >
    > - --
    > Lew Pitcher
    > IT Specialist, Enterprise Data Systems,
    > Enterprise Technology Solutions, TD Bank Financial Group
    >
    > (Opinions expressed are my own, not my employers')
    > -----BEGIN PGP SIGNATURE-----
    > Version: GnuPG v1.2.4 (MingW32)
    >
    > iD8DBQFDeNn9agVFX4UWr64RApO1AKDE/JG9TDcc/cZW8BMRa50QRpPfgwCfdhzH
    > zAFPpJRU1XqYJ8M/BkHpJz4=
    > =gSJQ
    > -----END PGP SIGNATURE-----



  4. Re: a simple shell script / crontab question

    On 2005-11-14, Lew Pitcher wrote:
    >
    > yerk5@hotmail.com wrote:
    > [snip]
    >> go=`cat ~/flag`


    Useless Use of Cat:

    read go < ~/flag

    >> if [ $go == 1 ]


    There is no good reason to use == instead of -.

    if [ "$go" = 1 ]

    >> then
    >> cp ~/flag ~/flag2
    >> fi
    >>
    >>
    >> If I run this script from the shell prompt, it works exactly as
    >> expected. However if I set crontab to run it while I'm not logged in,
    >> it doesnt work and I come back to email from cron saying:
    >>
    >> [: 0: unexpected operator
    >>
    >> What's going on here? TIA.

    >
    > At a guess, cron uses a different shell than you do at the command prompt
    >
    > Some shells have the '[' command built in, and some don't


    All shells since 198?, if not since 197[7-9], have [ builtin.

    > The built-in version of '[' recognizes the == equality test


    In most shells it does not. The equality test is =, not ==.

    > The command version (an alias of the 'test' binary) does not


    True.

    > When you run your script at the commandline, you probably get the builtin
    > version of [, which recognizes == as valid


    Pure serendipity.

    > When you run your script through cron, you probably get a different shell, which
    > defaults to the command version of [, which in turn does not recognize == as valid


    It may use the built-in version of [, which also may not recognize ==.

    > Try prepending a hashbang to your script, pointing it at the proper shell
    > interpreter. Something like...
    >
    > #!/bin/bash


    Better still, use the portable syntax, with a single =

    > go=`cat ~/flag`


    See above.

    > if [ $go == 1 ]


    if [ $go = 1 ]

    ... which works everywhere.

    > then
    > cp ~/flag ~/flag2
    > fi



    --
    Chris F.A. Johnson, author |
    Shell Scripting Recipes: | My code in this post, if any,
    A Problem-Solution Approach | is released under the
    2005, Apress | GNU General Public Licence

  5. Re: a simple shell script / crontab question

    Chris F.A. Johnson wrote:

    > > Some shells have the '[' command built in, and some don't

    >
    > All shells since 198?, if not since 197[7-9], have [ builtin.


    (only early traditional shells miss it: it was added in System III, '81.
    So nowadays it's only relevant in Ultrix' /bin/sh (but not /bin/sh5)
    and bourne shells from socalled "bsd universes" on some unix variants)

  6. Re: a simple shell script / crontab question

    2005-11-15, 21:58(+01), Sven Mascheck:
    > Chris F.A. Johnson wrote:
    >
    >> > Some shells have the '[' command built in, and some don't

    >>
    >> All shells since 198?, if not since 197[7-9], have [ builtin.

    >
    > (only early traditional shells miss it: it was added in System III, '81.
    > So nowadays it's only relevant in Ultrix' /bin/sh (but not /bin/sh5)
    > and bourne shells from socalled "bsd universes" on some unix variants)


    Yes, but there are occurences where it is the "[" binary command
    that is used.

    Mainly, when an utility is called by another command via the
    "execle" system call, rather than via the system(3) function.

    Like in:

    env -i [ a = b ]
    find . -exec [ -f {} ] \; -print

    and so on (poor examples, I conceed).

    Another good reason to stick to a standard syntax.

    --
    Stéphane

  7. Re: a simple shell script / crontab question

    Stephane CHAZELAS wrote:

    > but there are occurences where it is the "[" binary command that is used.


    ....if a "[" command is available at all. There's none on several unix
    variants (e.g. Solaris, HP-UX, AIX), because it originates from BSD.

+ Reply to Thread