0

I want to put an interface down and up every 1 second for 80 times, how can I implement this by a bash script?

Something like this?

COUNT = 80
for n in $(seq -w 1 $COUNT); do
    case $n in   
    [1,3,5,7,9....79]*) # I don't know how to represent the odd value only
       ifconfig veth1 down
       sleep 1
       ;;
    [2,4,6,8,10....80]*)
       ifconfig veth1 up
       sleep 1
       ;;
   esac
done 
Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
manxing
  • 3,165
  • 12
  • 45
  • 56
  • Have you tried it? Did it work? If not, what error do you get? What's the expected and actual output, if any? Please edit your question accordingly. – Some programmer dude Jun 21 '12 at 11:48

4 Answers4

3
COUNT=40
for n in $(seq -w 1 $COUNT); do
  ifconfig veth1 down
  sleep 1
  ifconfig veth1 up
  sleep 1
done

Or if you really want to count to 80:

COUNT=80
for n in $(seq -w 1 $COUNT); do
  case $n in
    *[13579])
     ifconfig veth1 down
     ;;
    *)
     ifconfig veth1 up
     ;;
  esac
  sleep 1
done
ghoti
  • 45,319
  • 8
  • 65
  • 104
3

Toggle a flag:

#!/bin/bash
for ((i = 1, flag = 0; i <= 80; i++))
do
    if ((flag ^= 1))
    then
        ifconfig veth1 down    # odd
    else
        ifconfig veth1 up
    fi
    sleep 1
done
Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
2

use % operator. like the following, replace the echo with the commands you want

count=0
while [ $count -lt 80 ]
do
    if (( $count % 2 == 0 ))
    then
        echo 'aaa'
    else
        echo 'bbb'
    fi

    count=$(( $count + 1 ))
done
Fivesheep
  • 236
  • 2
  • 7
  • 1
    `while (( count < 80 ))` and `(( count++ ))` but wouldn't you really rather let Bash maintain the counter? `for ((count = 0; count < 80; count++))` – Dennis Williamson Jun 21 '12 at 16:39
0

If you don't mind the bashisms, you can make your code much more concise through the use of various expansions available in Bash. For example:

for i in {1..80}; do
    case $((i % 2)) in
        0) ifconfig veth1 down ;;
        1) ifconfig veth1 up   ;;
    esac
    sleep 1
done

The magic here is the {1..80} sequence expression, coupled with the modulo operator to determine whether the number is odd or even. If your version of Bash doesn't support sequence expressions for any reason, just use $(seq 1 80) instead.

Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
  • Sequence expressions were introduced in Bash 3 almost 8 years ago. – Dennis Williamson Jun 21 '12 at 16:36
  • @DennisWilliamson You're right...it was the sequence expression *increment* that was added in Bash 4. Since my example doesn't use sequence increments, I'll update the answer. – Todd A. Jacobs Jun 21 '12 at 23:12
  • Sequence expressions are nice as far as they go, but they don't seem to be able to use variables. That is, `c=4; echo {1..$c}` does not return `1 2 3 4`, but `{1..4}`. You would need to `eval` this to expand it. And if `eval` is the answer..... So anyway, +1 for `$((i % 2))`, but -1 for `{1..80}` because of the loss of flexibility. – Graham Jun 26 '12 at 21:33