Shell script to check files if exist else touch the file

Hi All,
Thanks in Advance

I wrote the following code

 if [ $VERSION == 1.1 ]
    then 
       echo "version is 1.1"
       for i in "subscriber promplan mapping dedicatedaccount faflistSub faflistAcc accumulator pam_account"
       do
           FILE="SDP_DUMP_$i.csv"
           echo "$FILE"
           if [ -e "$FILE" ]
              then
                echo "$FILE Exists"
           else
                echo "Creating $FILE"
                touch $FILE
           fi
           i=`expr $i + 1`
       done
 else
     echo "version is wrong"
 fi

there are some 8 files in the directory

DUMP_accumulator.csv
DUMP_dedicatedaccount.csv
DUMP_faflistAcc.csv
DUMP_faflistSub.csv
DUMP_mapping.csv
DUMP_promplan.csv
DUMP_subscriber.csv
DUMP_pam_account.csv

it has the common prefix "DUMP_" and suffix as ".csv"
so i used a for loop to check for matching the filenames
but i am getting error in touch command as

Creating
touch: missing file operand
Try `touch --help' for more information.
expr: syntax error

and also error in expr

and the extension .csv is not appending to the files
and its creating only DUMP_subscriber and other files without extension .csv

Kindly assist

Hi,

Whats your output of

echo "Creating $FILE"

in else part. just comment the touch and execute it.
I think, $FILE doesn't have any value in it.
Cheers,
Ranga :slight_smile:

what is your output ?

for i in "subscriber promplan mapping dedicatedaccount faflistSub faflistAcc accumulator pam_account"

I think you will have only one loop with i="subscriber promplan mapping dedicatedaccount faflistSub faflistAcc accumulator pam_account"

so the script do a

touch 
SDP_DUMP_subscriber promplan mapping dedicatedaccount faflistSub faflistAcc accumulator pam_account.csv

Try with this for:

for i in subscriber promplan mapping dedicatedaccount faflistSub faflistAcc accumulator pam_account

What is the expr for? If you want to loop around all those files you could use:
for i in subscriber promplan mapping dedicatedaccount faflistSub faflistAcc accumulator pam_account
(without quotes)

Hi
my output is to be like

$FILE = DUMP_subscriber.csv
then it will check in the directory if that file exist or not.
if not it will touch and create a file DUMP_subscriber.csv
in the code
FILE="SDP_DUMP_$i.csv"
for each counter it will create one file as in the 8
DUMP_accumulator.csv
DUMP_dedicatedaccount.csv
DUMP_faflistAcc.csv
DUMP_faflistSub.csv
DUMP_mapping.csv
DUMP_promplan.csv
DUMP_subscriber.csv
DUMP_pam_account.csv

and will check all the eight files
if its not exist it will create the file

Thanks

If its just add SDP_ to existing files then :

    for i in *.csv
    do
       #FILE="SDP_DUMP_$i.csv"
       FILE="SDP_DUMP_$i"      # otherwise you will end up with e.g.  SDP_DUMP_DUMP_mapping.csv.csv
       echo FILE: "$FILE"
              if [ -e "$FILE" ]
       then
           echo "$FILE Exists"
       else
           echo "Creating $FILE"
           touch $FILE
        fi
        #i=`expr $i + 1`    # You cant add litteral with numeric... (expression error you had...)
        #  and There is not need to increment or change value, for does it automatically... 
     done