Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

script problem

Status
Not open for further replies.

gazwalker

Programmer
Apr 24, 2001
11
GB
Hi all go easy on me as im still getting the hang of this

iv got a bash script that will bundle a few files together. to run it you enter somthing like this

bundle house home light > newfile

this will put the 3 files "house home light" together in the newfile.

I got it doing that but now need to add some error checking.

im trying to make the script detect if the user tries to add a "/" character in any of the parameters and if so output an error message.

how would i go about checking the parameters to see if they contain the '/' character ?

thanx all for your help
 
I assume that your script takes exactly 3 arguments (being the files needed to be packed into a 4th.

You can test for existance of a file as non-directory using -f filename and for existance of arguments given to your script using $# (being the number of arguments given.

#use your desired output file name
myOutputFile=XXX

#check for arguments
if [ $# -eq 0 ]; then; echo "no arguments"; exit; fi
if [ $# -gt 3 ]; then; echo "too many arguments"; exit; fi

#do only if files/arguments exist
if [ -f $1 ]; then
if [ -f $2 ]; then
if [ -f $3 ]; then
#all 3 arguments are non-directories
cat $1 $2 $3 > myOutputFile
else; echo "3rd argument missing"
fi
else; echo "2nd argument missing"
fi
else; echo "1st argument missing"
fi
 
sorry for the quick & dirty approach, I was in a bit of a hurry and since I cannot edit my post, I'll try and make it better 2nd time:


#use your desired output file name
myOutputFile=XXX

#check for arguments
if [ $# -eq 0 ]
then
echo "no arguments"; exit
fi
if [ $# -gt 3 ]
then
echo "too many arguments"; exit
fi

#do only if files/arguments exist
if [ -f $1 ]; then
if [ -f $2 ]; then
if [ -f $3 ]; then
#all 3 arguments are non-directories
cat $1 $2 $3 > $myOutputFile
else
echo "3rd argument not a file"
fi
else
echo "2nd argument not a file"
fi
else
echo "1st argument not a file"
fi


Of course you could do a lot more testing. Test if the output file exists and act accordingly. Cater for less than 3 arguments, etc. but I hope you got the idea ...
 
thanx bud that will get me going i think now, i firgured out most of it now,

-----

just finished it half way through this post thanks bud
 
This would take any number of parameters...
Code:
#!/bin/ksh

if [[ $# -eq 0 ]] ; then print "No arguments given"; exit; fi

for FNAME in $*
do
   if [[ -f ${FNAME} ]]
   then
      # It's a good file
      cat ${FNAME}
   else
      # It's not a file
      print -u2 "ERROR: ${FNAME} is not a file!"
   fi
done
If the script is called [tt]bundle[/tt], then you would run it as...
[tt]
bundle house home light > newfile
[/tt]
Any errors reported go to stderr and won't show up in the [tt]newfile[/tt] that's created.

Hope this helps.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top