Difference between revisions of "Bash examples"
From Teknologisk videncenter
m |
m |
||
Line 5: | Line 5: | ||
pre { font-weight: bold; font-size: 150%;} | pre { font-weight: bold; font-size: 150%;} | ||
}} | }} | ||
− | + | = bash builtins = | |
− | + | == if command == | |
− | + | === Greetings === | |
<div style="margin: 10px 100px"> | <div style="margin: 10px 100px"> | ||
<source lang="bash"> | <source lang="bash"> | ||
Line 34: | Line 34: | ||
</source> | </source> | ||
</div> | </div> | ||
− | + | == while == | |
− | + | === count example === | |
+ | <div style="margin: 10px 100px"> | ||
<source lang="bash"> | <source lang="bash"> | ||
#!/bin/bash | #!/bin/bash | ||
Line 45: | Line 46: | ||
done | done | ||
</source> | </source> | ||
− | + | </div> | |
− | + | == case == | |
+ | === secret password example === | ||
+ | <div style="margin: 10px 100px"> | ||
<source lang="bash"> | <source lang="bash"> | ||
#!/bin/bash | #!/bin/bash | ||
Line 81: | Line 84: | ||
echo "The secret information is 198273" | echo "The secret information is 198273" | ||
</source> | </source> | ||
− | + | </div> | |
− | + | == for == | |
+ | === "ADD" users example === | ||
+ | <div style="margin: 10px 100px"> | ||
<source lang="bash"> | <source lang="bash"> | ||
#!/bin/bash | #!/bin/bash | ||
Line 95: | Line 100: | ||
done | done | ||
</source> | </source> | ||
+ | </div> |
Revision as of 11:11, 21 May 2009
Contents
bash builtins
if command
Greetings
#!/bin/bash
#Uncomment HOUR=`date +%H` when using for real
#HOUR=`date +%H`
#
#HOUR=$1 only for testing purposes.....
HOUR=$1
if test $HOUR -le 7
then
echo "You are early go home"
else if test $HOUR -le 8
then
echo "You are early"
else if test $HOUR -lt 12
then
echo "Good morning, Vietnam"
else if test $HOUR -lt 16
then
echo "Good evening"
fi
fi
fi
fi
while
count example
#!/bin/bash
COU=1
while test $COU -lt 10000
do
echo -en "COU = $COU\r"
COU=`expr $COU + 1`
done
case
secret password example
#!/bin/bash
# Read the username from the terminal
echo -en "Enter username: "
read USERNAME
# Read the password from the terminal not echoing the characters
echo -en "Enter password: "
stty -echo
read PASSWORD
stty echo
# Find the username in the case and check password
case $USERNAME in
john|John) if test $PASSWORD = "banana"
then
echo "Access granted"
else
echo "Access denied...."
exit
fi
;;
eve|Eve) if test $PASSWORD = "apple"
then
echo "Access granted"
else
echo "Access denied...."
exit
fi
;;
*) echo "Access denied...."
exit
;;
esac
echo "The secret information is 198273"
for
"ADD" users example
#!/bin/bash
USERLIST="bob eve john henry george jane ursula"
for USER in $USERLIST
do
echo -en "Creating user $USER"
# Next three line just pretend to add a user. Just playing :-)
echo "user $USER login permitted" >> /tmp/allowedUsers
sleep 1
echo -e "....[DONE]"
done