Tags

,

This script has the following,
– for loop
– if
– variable
– check file existence
– printf to adding leading zeros
– dereference a variable with echo

notes:
– space sensitivity for some keyword
– beware ` vs ‘ vs ”
– ${var} can be w/o {} if by itself

#!/bin/bash
for id in {1..100}
do
        # no space before after '='
        id2=$(echo `printf %03d $id`);   # printf to format to 3 digits
        file=datafile${id2};
        echo raw file name: '$file';

        # must use space after [ and before ]
        if [ -r "$file.dat" ] ;          # file.ext exist?
        then
            echo $file exists
        fi
done

reference:

Advertisements