Search code examples
kshaix

How to Include Variables in a File Name used in a Variable $File Equals


Why does the echo not return - /lsf10/monitors/lpstat_email_1_vmobius_05122021.txt ? It returns --> the $FILE

#!/usr/bin/ksh
integer max=3
integer i=1
while [[ $i -lt $max ]]
do
today=`date +%m%d%Y`
FILE = "/lsf10/monitors/lpstat_email_$i_vmobius_$today.txt"
echo "the $FILE"

echo $i
echo "the $FILE"
(( i = i + 1 ))
done
 

Solution

  • Use ${...} around variables for interpolation inside string literals - it makes things clearer and helps the interpreter.

    Indentation helps readability and maintainability.

    Try this:

    #!/usr/bin/ksh
    integer max=3
    integer i=1 
    while (( i < max ))
    do
        today=`date +%m%d%Y`
        FILE="/lsf10/monitors/lpstat_email_${i}_vmobius_${today}.txt"
        echo "the $FILE"
        echo $i
        echo "the $FILE"
        (( i ++ ))
    done