Find the answer to your Linux question:
Results 1 to 3 of 3
hi guys, recently i've been building an automated backup script for our medical systems that run a version of linux on them. i've had some success but cannot get "if" ...
  1. #1
    Linux User cayalee's Avatar
    Join Date
    Sep 2004
    Location
    england
    Posts
    389

    problems with "if"

    hi guys,
    recently i've been building an automated backup script for our medical systems that run a version of linux on them.
    i've had some success but cannot get "if" commands to work at all.
    whereever they pop up, i get:
    Code:
    "if" syntax command
    or something similar as an error message. commented out the script runs fine, but loses a nice touch of "detecting" which programs are installed.
    can anyone give me any advice?

    i've attatched the script as a .txt
    thanks in advance!
    Attached Files Attached Files
    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
    Just Joined!
    Join Date
    Feb 2009
    Posts
    45
    <Edit time="2009-03-10T08:33:26.790836095+02:00" reason="Correction">
    This answer is wrong and doesnʼt solve the problem in question.
    </Edit>

    Quote Originally Posted by cayalee
    i've had some success but cannot get "if" commands to work at all.
    whereever they pop up, i get:
    Code:
    "if" syntax command
    or something similar as an error message.
    […]
    Code:
    #if [ ! -d "$BACKUPDIR" ]
    #   then
    #   echo "ERROR Couldn't find usb drive mounted at /mnt/#usbdisk. please mount to this directory"
    #   exit 1
    #fi
    Itʼs simply a matter of a forgotten semicolon, because bourne shellʼs proper «if» construct syntax is:
    Code:
    if [ «CONDITION» ]; then
    	«CODE»
    fi
    For more information about bashʼs syntax:
    Code:
    $> man bash
    […]
           if list; then list; [ elif list; then list; ] ... [ else list; ] fi
                  The  if  list is executed.  If its exit status is zero, the then list is executed.  Otherwise, each elif list is executed in turn, and if its exit status is zero, the
                  corresponding then list is executed and the command completes.  Otherwise, the else list is executed, if present.  The exit status is the exit status of the last com‐
                  mand executed, or zero if no condition tested true.
    
    […]

  3. #3
    Linux Newbie
    Join Date
    Mar 2009
    Posts
    228
    I don't think it's a matter of missing semi-colons when the 'then' is on a separate line. The problem is further down the script, for example:

    Code:
    [ -d /usr/people/cranial ]
     then
    echo "found Synergy Cranial. Preparing to back up"
    cp /usr/people/cranial/cranialData/* /mnt/usbdisk/cranialData
    echo "done"
     else
    echo "synergy cranial not installed, skipping step"
    endif

    Which should be:

    Code:
    if [ -d /usr/people/cranial ]
     then
    echo "found Synergy Cranial. Preparing to back up"
    cp /usr/people/cranial/cranialData/* /mnt/usbdisk/cranialData
    echo "done"
     else
    echo "synergy cranial not installed, skipping step"
    fi

Posting Permissions

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