...

View Full Version : shell help for a beginner



tasslehawf
08-26-2007, 10:40 PM
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!

ghostdog74
08-27-2007, 04:41 AM
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 :
cd `echo $PATH` can be written as
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...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum