Difference between revisions of "BASH"
Jump to navigation
Jump to search
Rafahsolis (talk | contribs) |
Rafahsolis (talk | contribs) |
||
| Line 65: | Line 65: | ||
=== User Input === | === User Input === | ||
| + | <source lang="bash"> | ||
| + | #!/bin/bash | ||
| + | while true; do | ||
| + | read -p "Do you wish to install this program?" yn | ||
| + | case $yn in | ||
| + | [Yy]* ) make install; break;; | ||
| + | [Nn]* ) exit;; | ||
| + | * ) echo "Please answer yes or no.";; | ||
| + | esac | ||
| + | done | ||
| + | </source> | ||
| + | |||
=== Arrays === | === Arrays === | ||
=== Functions === | === Functions === | ||
Revision as of 00:58, 27 March 2015
BASH Script
Hello World
#!/bin/bash
STR="Hello World"
echo $STR
Variables
Local Variables
#!/bin/bash
HELLO=Hello
function hello {
local HELLO=World
echo $HELLO
}
echo $HELLO
hello
echo $HELLO
Strings
Loops
While
#!/bin/bash
COUNTER=0
while [ $COUNTER -lt 10 ]; do
echo The counter is $COUNTER
let COUNTER=COUNTER+1
done
For
#!/bin/bash
for i in $( ls ); do
echo item: $i
done
Conditional
if
#!/bin/bash
T1="foo"
T2="bar"
if [ "$T1" = "$T2" ]; then
echo expresion evaluated as true
else
echo expresion evaluated as false
fi
case
#!/bin/bash
case $yn in
[YySs]* ) adduser $user; break;;
[Nn]* ) echo no se creo usuario $user; break;;
* ) echo "Elije y/n";;
esac
User Input
#!/bin/bash
while true; do
read -p "Do you wish to install this program?" yn
case $yn in
[Yy]* ) make install; break;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done
Arrays
Functions
<source lang="bash"> function quit {
exit
} function e {
echo $1
} e Hello e World quit echo foo </bash>