# Script Help

**URL:** <https://community.unix.com/t/script-help/204669>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [July 12, 2008, 7:18pm UTC](https://community.unix.com/t/script-help/204669 "2008-07-12T19:18:14Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![findprakash](https://community.unix.com/letter_avatar/findprakash/32/6_22734026db12803ebb3e059622ac3534.png) [@findprakash](https://community.unix.com/u/findprakash)\
**Post date:** [July 12, 2008, 7:18pm UTC](https://community.unix.com/t/script-help/204669/1 "2008-07-12T19:18:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![fpmurphy](https://community.unix.com/user_avatar/community.unix.com/fpmurphy/32/110_2.png) [@fpmurphy](https://community.unix.com/u/fpmurphy)\
**Post date:** [July 12, 2008, 9:01pm UTC](https://community.unix.com/t/script-help/204669/2 "2008-07-12T21:01:02Z")

</div>

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

Lets start with your loop.

```nohighlight
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:

```nohighlight
i=5

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

```

---

<div class="post-metadata">

**Author:** ![findprakash](https://community.unix.com/letter_avatar/findprakash/32/6_22734026db12803ebb3e059622ac3534.png) [@findprakash](https://community.unix.com/u/findprakash)\
**Post date:** [July 12, 2008, 9:31pm UTC](https://community.unix.com/t/script-help/204669/3 "2008-07-12T21:31:33Z")

</div>

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}`

---

<div class="post-metadata">

**Author:** ![crystal\_pup](https://community.unix.com/letter_avatar/crystal_pup/32/6_22734026db12803ebb3e059622ac3534.png) [@crystal\_pup](https://community.unix.com/u/crystal_pup)\
**Post date:** [July 12, 2008, 9:57pm UTC](https://community.unix.com/t/script-help/204669/4 "2008-07-12T21:57:57Z")

</div>

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}' `

---

<div class="post-metadata">

**Author:** ![findprakash](https://community.unix.com/letter_avatar/findprakash/32/6_22734026db12803ebb3e059622ac3534.png) [@findprakash](https://community.unix.com/u/findprakash)\
**Post date:** [July 12, 2008, 10:14pm UTC](https://community.unix.com/t/script-help/204669/5 "2008-07-12T22:14:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![crystal\_pup](https://community.unix.com/letter_avatar/crystal_pup/32/6_22734026db12803ebb3e059622ac3534.png) [@crystal\_pup](https://community.unix.com/u/crystal_pup)\
**Post date:** [July 12, 2008, 10:27pm UTC](https://community.unix.com/t/script-help/204669/6 "2008-07-12T22:27:39Z")

</div>

Hi,

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

Cheers,  
Kunal

---

<div class="post-metadata">

**Author:** ![findprakash](https://community.unix.com/letter_avatar/findprakash/32/6_22734026db12803ebb3e059622ac3534.png) [@findprakash](https://community.unix.com/u/findprakash)\
**Post date:** [July 12, 2008, 10:39pm UTC](https://community.unix.com/t/script-help/204669/7 "2008-07-12T22:39:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![findprakash](https://community.unix.com/letter_avatar/findprakash/32/6_22734026db12803ebb3e059622ac3534.png) [@findprakash](https://community.unix.com/u/findprakash)\
**Post date:** [July 12, 2008, 10:41pm UTC](https://community.unix.com/t/script-help/204669/8 "2008-07-12T22:41:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![fpmurphy](https://community.unix.com/user_avatar/community.unix.com/fpmurphy/32/110_2.png) [@fpmurphy](https://community.unix.com/u/fpmurphy)\
**Post date:** [July 13, 2008, 9:05am UTC](https://community.unix.com/t/script-help/204669/9 "2008-07-13T09:05:56Z")

</div>

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

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

```
