Script Help

I have file standardfilecleanup.lst which has contents as below :

db:background_dump_dest:alert.log:test.log:rest:log
db:user_dump_dest:best.log:test.log
db:core_dump_dest:test.log

Below is my script :

#set -xv

i=5

while [ $i -le 3 ]
do
var_value="$"`echo $i`
file_name=`cat standardfilecleanup.lst|awk -F: {'print ${var_value}`
echo ${file_name}
i=`expr $i + 1`
done

When I run my script it is erroring out on below line.

file_name=`cat standardfilecleanup.lst|awk -F: {'print ${var_value}`

My expectation is :

when $var_value is $3 file_name variables value should be alert.log.
when $var_value is $4 file_name variables value should be test.log.
when $var_value is $5 file_name variables value should be rest:log.

Any help is greatly appreciated.

Unfortunately everything about your script seems to have something wrong with it.

Lets start with your loop.

i=5

while [ $i -le 3 ]
do
    ......
    i=`expr $i + 1`
done

As written you will never enter the while loop since i is always greater than 3. If you want to count down from 5 to 3, then you need to do something like:

i=5

while [[ $i > 2 ]]
do
    .....
    i=`expr $i - 1`             
done

My bad. While elaborating my problem I made change but did it at wrong place. while loop in script does not have any issue.

#set -xv

i=3

while [ $i -le 5 ]
do
var_value="$"`echo $i`
file_name=`cat standardfilecleanup.lst|awk -F: {'print ${var_value}`
echo ${file_name}
i=`expr $i + 1`
done

Below statement is erroring out :

file_name=`cat standardfilecleanup.lst|awk -F: {'print ${var_value}`

I believe this command is not properly formatted -->
file_name=`cat standardfilecleanup.lst|awk -F: {'print ${var_value}`

Try this :

file_name=`cat standardfilecleanup.lst | awk -F ":" 'print ${var_value}' `

Thanks for the update cystal. Below command is erroring out.

file_name=`cat standardfilecleanup.lst | awk -F ":" 'print ${var_value}' `

awk -F: -> tells that : is feild separator.

I don't know how to code so that {'print ${var_value}' command translates to {'print $3'}.

Thanks,
Prakash

Hi,

Try this :
file_name=`cat standardfilecleanup.lst | awk -F ":" '{print $'$var_value'}'`

Cheers,
Kunal

Thanks Crystal. After making changes command is working fine but not giving expected output. Output of script below.

# ./testfile
File NAme db:background_dump_dest:alert.log:test.log:rest:log

#

Expected output would be:

File NAme alert.log:test.log:rest:log
File NAme test.log:rest:log
File NAme rest:log

Thanks,
Prakash

Crystal after changing command as below its working as expected.

file_name=`cat standardfilecleanup.lst|awk -F: '{print '$var_value'}'`

Thanks for your valuable input.

Thanks,
Prakash

You can reduce the number of lines of code by using a for loop and directly printing output from command substitution.

The following works for both bash and ksh93

for ((i=3;i<=5;i++))
do
     print $(awk -F: -v var=$i '{print $var}' standardfilecleanup.lst)
done