Find the answer to your Linux question:
Results 1 to 5 of 5
hi guys, been absent from here for a while but need some help with creating a script. i've been trying to build a shell script that tests to see if ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux User cayalee's Avatar
    Join Date
    Sep 2004
    Location
    england
    Posts
    389

    problems with "if"


    hi guys, been absent from here for a while but need some help with creating a script. i've been trying to build a shell script that tests to see if a folder is present and if it is, then copy it to a predefined backup location.
    however, it just keeps giving me the following error
    Code:
    if: Expression Syntax
    so the way i've built it is like this:
    Code:
    if [ -d /usr/people/cranial4 ]
    then
    echo "found cranial, preparing to back up"
    cp /usr/peoplecranial /mnt/usbdisk/cranialdata
    else
    echo "cranial not installed, skipping step"
    fi
    as i understand it, using [ ] is the same as using the test command, and the -d option specifies its a directory. i want it to check whether a program is installed, and if so copy some files. if not, tell me that its not installed and move on.
    where am i going wrong? i'm bouncing my head off the wall
    thanks in advance guys
    You know, aliens are going to come to earth in 50 years and kill the hell out of us for DDoSing their networks with this SETI crap
    registered linux user #388463

  2. #2
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    very strange, I tested script
    Code:
    #!/bin/bash
    if [ -d /home/ccooper ]
    then
    echo "1"
    else
    echo "0"
    fi
    very similar to yours, works for me

  3. #3
    Linux User cayalee's Avatar
    Join Date
    Sep 2004
    Location
    england
    Posts
    389
    it is very weird, i've been building this on ubuntu/DSL and then trying to run it on the work linux (2.4.1 kernel) and it doesn't like it. i can build a similar script using same commands etc on the work machine and it plays nicely..
    no idea what the issue is but need to do a rebuild by the looks..
    cheers!
    You know, aliens are going to come to earth in 50 years and kill the hell out of us for DDoSing their networks with this SETI crap
    registered linux user #388463

  4. $spacer_open
    $spacer_close
  5. #4
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    I suspect it's not the problem of the kernel. I think it has something to do with the default shell on your work computer. If I'm not mistaken, it is either:
    1. a line #!/bin/csh at the beginning of the script in question,
    2. the default shell is csh on the work computer [ use echo $SHELL to find out ]and the shebang line #!/bin/bash is missing.

    To replicate the problem, change the shebang line on the scripts listed on this page so far to #!/bin/csh and run the script in this way: (script needs to be made executable first)
    Code:
    ./script
    I think it's a good habit to specify which shell you will be using in the script otherwise problems like this will surface.

    Reference: if: Expression Syntax. Blog of Annoying Technical Details

  6. #5
    Linux User cayalee's Avatar
    Join Date
    Sep 2004
    Location
    england
    Posts
    389
    thankyou very much for this, most usefull!
    You know, aliens are going to come to earth in 50 years and kill the hell out of us for DDoSing their networks with this SETI crap
    registered linux user #388463

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •