PDA

View Full Version : [SOLVED] [bash] File validation



xsinsx
June 28th, 2010, 05:11 PM
I was curious how i might test if a file exists or whether the argument is a filename at all.


#!/bin/bash

i=1 #counter variable
new_file=1 # new text file labler
headarg=$1 # head command argument
FILE_NAME=$2 # file argument used in chop
PARAMETERARG=$3 # the number of times you want this chomped
NUMOARGS=3 # required arguments
E_ARGS=65 # exit error for insufficent arguments
E_INT=68 # exit error for not placing and integer as an argument


if [ $# -ne $NUMOARGS ] # Checks for number of arguments
then
echo "Usage: `basename $0` Arg1 Arg2 Arg3"
echo "Insufficent Arguments"
exit $E_ARGS
fi


if [[ ! -z $(echo $headarg | sed 's/[0-9]//g') ]]
then
echo "Your argument must be a integer value number"
exit $E_INT
fi


if [[ ! -z $(echo $PARAMETERARG | sed 's/[0-9]//g') ]]
then
echo "Your argument must be an integer value number"
exit $E_INT
fi


while [ $i -ne $PARAMETERARG ] # main funtion
do
head -$headarg $FILE_NAME | tail -$1 > $new_file.txt
headarg=$(( $headarg + $1 ))
i=$(( $i + $1 ))
filename=$(( $filename + 1 ))
done

MadCow108
June 28th, 2010, 07:15 PM
man test

there are lots of flags for file testing, most important -f and -e

xsinsx
June 28th, 2010, 07:20 PM
Thanks for your reply i was hoping that someone would just point me in the right direction thank you.