stupid human tricks

So, my memories shot to hell and back and some things are next to impossible to hold in there. Of course, bash scripting requires memorization 🙂
So, I slapped together a quick “cheat sheet” that pretty much is what I remembered before but can’t now (easily). I keep it as a quick go-to at my desk. Figured I’d share.

for loop syntax

for VARIABLE in 1 2 3 4 5 .. N

do

command1

command2

commandN

done

for i in 1 2 3 4 5

do

echo “Welcome $i times”

done

for i in $(seq 1 2 20)

do

echo “Welcome $i times”

done

for i in {1..5}

do

echo “Welcome $i times”

done

for (( EXP1; EXP2; EXP3 ))

do

command1

command2

command3

done

for (( EXP1; EXP2; EXP3 ))

do

command1

command2

command3

done

for (( EXP1; EXP2; EXP3 ))

do

command1

command2

command3

done

for (( EXP1; EXP2; EXP3 ))

do

command1

command2

command3

done

for (( EXP1; EXP2; EXP3 ))

do

command1

command2

command3

done

for (( c=1; c<=5; c++ ))

do

echo “Welcome $c times…”

done

for (( ; ; ))

do

echo “infinite loops [ hit CTRL+C to stop]”

done

for I in 1 2 3 4 5

done

statements1      #Executed for all values of ”I”, up to a disaster-condition if any.

statements2

if (disaster-condition)

then

break #Abandon the loop.

fi

statements3     #While good and, no disaster-condition.

done

#!/bin/bash

for file in /etc/*

do

if [ “${file}” == “/etc/resolv.conf” ]

then

countNameservers=$(grep -c nameserver /etc/resolv.conf)

echo “Total  ${countNameservers} nameservers defined in ${file}”

break

fi

done

for I in 1 2 3 4 5

done

statements1      #Executed for all values of ”I”, up to a disaster-condition if any.

statements2

if (condition)

then

continue #Go to next iteration of I in the loop and skip statements3

fi

statements3

done

FILES=”$@”

for f in $FILES

do

if [ -f ${f}.bak ]

then

echo “Skiping $f file…”

continue

fi

/bin/cp $f $f.bak

done

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s