Quantcast
Channel: UNIX and Linux Forums
Viewing all articles
Browse latest Browse all 16232

for loop using {1..$t}

$
0
0
I read here: Bash For Loop Examples
that I should not use seq , instead use {1..10}

I am not getting this to work

old code
Code:

#!/bin/bash
t=$(cat /var/bin/servers | wc -l)
for c in $(seq 1 $t)
        do
                SERVER[$c]=$(awk 'NR==C { print $2 }' C="$c" /var/bin/servers)
        done

new code not working
Code:

#!/bin/bash
t=$(cat /var/bin/servers | wc -l)
for c in {1..$t}
        do
                SERVER[$c]=$(awk 'NR==C { print $2 }' C="$c" /var/bin/servers)
        done

This give error like this error token is "{1..8}"
{1..8} seems to be the correct replacement for $t

also tested this for c in {1..${t}} , not working

This works fine for c in {1..8} , but I need the variable option.

Viewing all articles
Browse latest Browse all 16232

Trending Articles