Find the answer to your Linux question:
Results 1 to 3 of 3
I'm new to c shell scripts and am currently trying to edit a script to include an if statement. I've created: cd $DIR_1 if (-e $DIR_rawdata/file-A*) then script1.pl else script2.pl ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jun 2012
    Posts
    1

    if statement not working properly


    I'm new to c shell scripts and am currently trying to edit a script to include an if statement. I've created:

    cd $DIR_1
    if (-e $DIR_rawdata/file-A*) then
    script1.pl
    else
    script2.pl
    endif


    If file-A* doesn't exist, then the script stops and gives the output 'no match'. I want it to run script2.pl if this file doesn't exist, and script1.pl if it does.

    I'm not sure what I'm doing wrong. Any advice would be appreciated.

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    i don't know much about c-shell, but you could do something hideous like this:

    Code:
    #!/bin/csh
    # here i assume the full directory path to the file is /tmp/foo/rawdata/file-A.whatever
    set DIR_1 = /tmp/foo
    cd $DIR_1
    set val = `echo ./rawdata/file-A*`
    if ( $val != "" ) then
      script1.pl
    else
      script2.pl
    endif
    Last edited by atreyu; 06-22-2012 at 11:38 PM. Reason: changed == to !=

  3. #3
    Linux User Krendoshazin's Avatar
    Join Date
    Feb 2005
    Location
    London, England
    Posts
    471
    Is the file actually called file-A*? I think you should reconsider the use of * in a filename as this is a wildcard, and it could cause a whole number of problems. If you accidentally put a space between file-A and * while using rm, then you can kiss all your files in that directory good bye. I'm fairly sure this is also why your script isn't working.

    Additionally, I would recommend using something such as BASH for your scripting needs. Here's a lengthy post on the lack of functionality of CSH: http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/
    Last edited by Krendoshazin; 06-23-2012 at 09:07 AM.

Posting Permissions

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