Difference between revisions of "BASH"

From RHS Wiki
Jump to navigation Jump to search
Line 1: Line 1:
== BASH Script ==
+
'''BASH Scripting'''
=== Hello World ===
+
== Hello World ==
 
<source lang="bash">
 
<source lang="bash">
 
#!/bin/bash
 
#!/bin/bash
Line 7: Line 7:
 
</source>
 
</source>
  
=== Variables ===
+
== Variables ==
==== Local Variables ====
+
=== Local Variables ===
 
<source lang="bash">
 
<source lang="bash">
 
#!/bin/bash
 
#!/bin/bash
Line 20: Line 20:
 
echo $HELLO
 
echo $HELLO
 
</source>
 
</source>
==== Boolean ====
+
=== Boolean ===
 
<source lang="bash">
 
<source lang="bash">
 
#!/bin/bash
 
#!/bin/bash
Line 30: Line 30:
 
</source>
 
</source>
  
=== Strings ===
+
== Strings ==
=== Loops ===
+
== Loops ==
==== While ====
+
=== While ===
 
<source lang="bash">
 
<source lang="bash">
 
#!/bin/bash
 
#!/bin/bash
Line 42: Line 42:
 
</source>
 
</source>
  
==== For ====
+
=== For ===
 
<source lang="bash">
 
<source lang="bash">
 
#!/bin/bash
 
#!/bin/bash
Line 49: Line 49:
 
done
 
done
 
</source>
 
</source>
==== C-like for ====
+
=== C-like for ===
 
<source lang="bash">
 
<source lang="bash">
 
#!/bin/bash
 
#!/bin/bash
Line 57: Line 57:
 
done
 
done
 
</source>
 
</source>
==== Until ====
+
=== Until ===
 
<source lang="bash">
 
<source lang="bash">
 
#!/bin/bash
 
#!/bin/bash
Line 67: Line 67:
 
</source>
 
</source>
  
=== Conditional ===
+
== Conditional ==
==== if ====
+
=== if ===
 
<source lang="bash">
 
<source lang="bash">
 
#!/bin/bash
 
#!/bin/bash
Line 80: Line 80:
 
</source>
 
</source>
  
==== case ====
+
=== case ===
 
<source lang="bash">
 
<source lang="bash">
 
#!/bin/bash
 
#!/bin/bash
Line 90: Line 90:
 
</source>
 
</source>
  
=== User Input ===
+
== User Input ==
 
<source lang="bash">
 
<source lang="bash">
 
#!/bin/bash
 
#!/bin/bash
Line 103: Line 103:
 
</source>
 
</source>
  
=== Arrays ===
+
== Arrays ==
 
<source lang="bash">
 
<source lang="bash">
 
ARRAY=(one two three)
 
ARRAY=(one two three)
Line 122: Line 122:
 
</source>
 
</source>
  
=== Functions ===
+
== Functions ==
 
<source lang="bash">
 
<source lang="bash">
 
function quit {
 
function quit {
Line 135: Line 135:
 
echo foo
 
echo foo
 
</source>
 
</source>
=== extras ===
+
== extras ==
==== Check if a package is installed ====
+
=== Check if a package is installed ===
 
<source lang="bash">
 
<source lang="bash">
 
#!/bin/sh
 
#!/bin/sh
Line 145: Line 145:
 
debInst terminator
 
debInst terminator
 
</source>
 
</source>
==== Check if a file exists ====
+
=== Check if a file exists ===
 
<source lang="bash">
 
<source lang="bash">
 
#!/bin/bash
 
#!/bin/bash
Line 162: Line 162:
 
</source>
 
</source>
  
==== Check user ====
+
=== Check user ===
 
<source lang="bash">
 
<source lang="bash">
 
#!/bin/bash
 
#!/bin/bash
Line 175: Line 175:
 
fi
 
fi
 
</source>
 
</source>
==== Execute localScript on remote server ====
+
=== Execute localScript on remote server ===
 
  <nowiki>ssh [user]@[server] 'bash -s' < [local_script]</nowiki>
 
  <nowiki>ssh [user]@[server] 'bash -s' < [local_script]</nowiki>

Revision as of 01:54, 27 March 2015

BASH Scripting

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

Boolean

#!/bin/bash
the_world_is_flat=true
# ...do something interesting...
if [ "$the_world_is_flat" = true ] ; then
    echo 'Be careful not to fall off!'
fi

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

C-like for

#!/bin/bash
for i in `seq 1 10`;
do
    echo $i
done

Until

#!/bin/bash
COUNTER=20
until [ $COUNTER -lt 10 ]; do
    echo COUNTER $COUNTER
    let COUNTER-=1
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

ARRAY=(one two three)

echo ${ARRAY[*]}
one two three

echo $ARRAY[*]
one[*]

echo ${ARRAY[2]}
three

ARRAY[3]=four

echo ${ARRAY[*]}
one two three four

Functions

function quit {
    exit
}
function e {
    echo $1
}
e Hello
e World
quit
echo foo

extras

Check if a package is installed

#!/bin/sh
debInst() {
    dpkg-query -Wf'${db:Status-abbrev}' "$1" 2>/dev/null | grep -q '^i'
}

debInst terminator

Check if a file exists

#!/bin/bash
if [ -d /opt/ ]; then
    echo "Sí, sí existe."
else
    echo "No, no existe"
fi

#-d -» Comprobar si existe determinado directorio
#-f -» Comprobar si existe determinado archivo
#-w -» Comprobar si determinado archivo tiene permisos de escritura
#-x -» Comprobar si determinado archivo tiene permisos de ejecución.

#if not: if [ ! -d /opt/ ]; then

Check user

#!/bin/bash
user=$(whoami)
if [ "$user" == "root" ]; then
   isroot=true
   echo "Eres Root! :)"
else
   isroot=false
   echo "No eres root... :'(" 
   exit
fi

Execute localScript on remote server

ssh [user]@[server] 'bash -s' < [local_script]