Find the answer to your Linux question:
Results 1 to 6 of 6
Hi guys, just having a bit of trouble using the "if" command in a script. i want a script that tests to see if a directory exists, and then if ...
  1. #1
    Linux User cayalee's Avatar
    Join Date
    Sep 2004
    Location
    england
    Posts
    389

    problems with if/else

    Hi guys, just having a bit of trouble using the "if" command in a script.
    i want a script that tests to see if a directory exists, and then if so, copy files from that directory to another drive. here's how i'm going about it, but it isn't working.
    any ideas?

    Code:
    if test -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
    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
    Super Moderator devils casper's Avatar
    Join Date
    Jun 2006
    Location
    Chandigarh, India
    Posts
    24,316
    Nothing is wrong in your code except cp command. If you are copying files then use wildcard. Use -r option for folder.
    To copy files from cranialData to other :
    Code:
    cp /usr/people/cranial/cranialData/* /mnt/usbdisk/cranialData
    If you want to copy cranialData folder to /mnt/usbstick :
    Code:
    cp -r /usr/people/cranial/cranialData /mnt/usbdisk
    It is amazing what you can accomplish if you do not care who gets the credit.
    New Users: Read This First

  3. #3
    Linux User cayalee's Avatar
    Join Date
    Sep 2004
    Location
    england
    Posts
    389
    thanks very much, i'll give this a try
    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. #4
    Linux User cayalee's Avatar
    Join Date
    Sep 2004
    Location
    england
    Posts
    389
    no joy i'm afraid. i just get an error message saying:
    if: Expression Syntax

    any ideas?
    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

  5. #5
    Super Moderator devils casper's Avatar
    Join Date
    Jun 2006
    Location
    Chandigarh, India
    Posts
    24,316
    I copied/paste your code ( changed folder_path only ) and its working fine here.
    It is amazing what you can accomplish if you do not care who gets the credit.
    New Users: Read This First

  6. #6
    Linux Newbie
    Join Date
    Feb 2009
    Posts
    99
    are you using bash env or csh env?
    because in bash --> if then else fi
    in csh --> if then else if


    and try

    #!/bin/bash
    if [ -d /usr/people/cranial ]
    then
    echo "found Synergy Cranial. Preparing to back up"
    cp -r /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
  •  
...