Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Aug 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    shell help for a beginner

    I have been programming in shell now for about a week.

    I am trying to write a script that will take a directory as an argument and then read and rename files. I am currently stuck at the part of error checking my directory argument.

    #! /bin/sh -f

    CURRENT_PATH=`pwd`
    #uses backticks to assign the current directory to $CURRENT_PATH

    PATH=$1
    #Assigns the imput string to $PATH

    if [ -z $PATH ]
    then
    echo 'no imput, using current path'
    echo $CURRENT_PATH
    PATH=$CURRENT_PATH
    # -z is checking to see if there was any imput

    elif [ -d $PATH ]
    then
    echo $PATH
    cd `echo $PATH`
    echo $PWD
    # -d is checking to see if the imput string was a directory

    else
    echo 'error'
    exit 1
    #error message if imput string is something other than a directory

    fi

    This particular part of functions fine in Darwin shell (MacOSX). In Ubuntu Linux, shell rejects 'elif':
    Syntax error: "elif" unexpected (expecting "then")

    I am having frustrating portibility problems. I started out using Ubuntu, but then my system crashed and I switched over to MacOSX and now I am working in both. The final program needs to run in Red Hat so I am thinking working in Ubuntu is my best bet (I have been able to replicate the same problem in Fedora Core).

    The reason I switched from OSX is that it was having problems with my 'find' and 'cut' commands. Testing them in single line programs worked ok, but not in the overall program.
    find: command not found
    cut: command not found

    I wonder if there is something I'm missing about the overall structure of my script:

    #! /bin/sh -f

    if []
    then
    ....
    elif []
    then
    ....
    else
    then
    ....
    fi

    find * |
    while read FILE
    do
    formatting
    output
    done


    Help!

  • #2
    New Coder
    Join Date
    May 2006
    Posts
    39
    Thanks
    0
    Thanked 1 Time in 1 Post
    Quote Originally Posted by tasslehawf View Post
    I have been programming in shell now for about a week.

    I am trying to write a script that will take a directory as an argument and then read and rename files. I am currently stuck at the part of error checking my directory argument.

    #! /bin/sh -f

    CURRENT_PATH=`pwd`
    #uses backticks to assign the current directory to $CURRENT_PATH

    PATH=$1
    #Assigns the imput string to $PATH

    if [ -z $PATH ]
    then
    echo 'no imput, using current path'
    echo $CURRENT_PATH
    PATH=$CURRENT_PATH
    # -z is checking to see if there was any imput

    elif [ -d $PATH ]
    then
    echo $PATH
    cd `echo $PATH`
    echo $PWD
    # -d is checking to see if the imput string was a directory

    else
    echo 'error'
    exit 1
    #error message if imput string is something other than a directory

    fi

    This particular part of functions fine in Darwin shell (MacOSX). In Ubuntu Linux, shell rejects 'elif':
    Syntax error: "elif" unexpected (expecting "then")

    I am having frustrating portibility problems. I started out using Ubuntu, but then my system crashed and I switched over to MacOSX and now I am working in both. The final program needs to run in Red Hat so I am thinking working in Ubuntu is my best bet (I have been able to replicate the same problem in Fedora Core).

    The reason I switched from OSX is that it was having problems with my 'find' and 'cut' commands. Testing them in single line programs worked ok, but not in the overall program.
    find: command not found
    cut: command not found

    I wonder if there is something I'm missing about the overall structure of my script:

    #! /bin/sh -f

    if []
    then
    ....
    elif []
    then
    ....
    else
    then
    ....
    fi

    find * |
    while read FILE
    do
    formatting
    output
    done


    Help!
    first of, this :
    Code:
    cd `echo $PATH`
    can be written as
    Code:
     cd $PATH
    . No need for the "echo" .

    your directory checking part works fine with bash, at least on my machine. For portability issues, the way i solve it is to write in a portable program language, such as Java, Python, Perl...etc...


  •  

    Posting Permissions

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