-
-
Save EsmailELBoBDev2/8200052f2c12478583208f899efe2b79 to your computer and use it in GitHub Desktop.
Bash Scripting Zero to Hero Examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#debugging shellscripts -x would show the scrips line by line and output followed by | |
#!/bin/bash -x | |
TEST_VAR="test" | |
echo "$TEST_VAR" | |
#custom | |
#set -x / start debugginh | |
#set +x / stop | |
#-ex exit on stop | |
#-v prints shell before substitution are applied | |
#-vx | |
#DEBUG="echo" | |
#$DEBUG ls |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#case statement example | |
read -p "Enter y or n:" ANSWER | |
case "$ANSWER" in | |
[yY]|[yY][eE][sS]) | |
echo "You answered yes." | |
;; | |
[nN]|[nN][oO]) | |
echo "You answered no." | |
;; | |
*) | |
echo "Invalid answer." | |
;; | |
esac |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#decisions | |
HOST_NAME="windows" | |
#simple if | |
if [ $HOST_NAME = "ubuntu" ]; then | |
echo "Ubuntu for humans" | |
fi | |
#if else | |
if [ $HOST_NAME = "ubuntu" ]; then | |
echo "Ubuntu for humans" | |
else | |
echo "Windows for !humans" | |
fi | |
#if elif else | |
if [ $HOST_NAME = "ubuntu" ]; then | |
echo "Ubuntu for humans" | |
elif [ $HOST_NAME = "windows" ]; then | |
echo "We !Love Windows" | |
else | |
echo "Windows for !humans" | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#declare variable | |
SERVER_NAME="nginx" | |
echo $SERVER_NAME | |
echo "We are running on $SERVER_NAME" | |
echo "We are ${SERVER_NAME}er"; | |
#command output to variable | |
SERVER_NAME=$(hostname) | |
echo "We are running on $SERVER_NAME" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#file operators | |
FILE="basic.sh" | |
if [ -e $FILE ]; then | |
echo "File Exists" | |
else | |
echo "No File Found" | |
fi | |
#-d,e,r,w,x | |
#string operations | |
#arithmetic operations are allowed | |
#arg1 -eq arg2 - equalto | |
#arg1 -ne arg2 - !equalto | |
#arg1 -lt arg2 - less than | |
#arg1 -le arg2 - less than equalto | |
#arg1 -gt arg2 - greater than | |
#arg1 -ge arg2 - greater than equalto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#syslog | |
#logger -p user.info "$MESSAGE" | |
#logger -i -t randomly -p user.info "$MESSAGE" | |
#while loop | |
while [ "$CORRECT" != "y" ] | |
do | |
read -p "Whats your name ? " NAME | |
read -p "Is your Name: $NAME - correct ?" CORRECT | |
done | |
#read file line by line | |
LINE_NUM=1 | |
while read LINE | |
do | |
echo "${LINE_NUM}: ${LINE}" | |
((LINE_NUM++)) | |
done < /etc/fstab |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#loops | |
COLORS="red green blue" | |
for COLOR in $COLORS | |
do |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#wildcards *.txt match zero or more characters | |
#?.txt match exactly one character | |
ls *\? #file end with ? | |
ls ? #file 1 character | |
ls -l *.txt | |
ls c[aeiou]t #character class | |
#can use this with mv cp rm | |
#copy .html files to another folder - wildcards | |
cd /var/www | |
for FILE in *.html | |
do | |
echo "Copying $FILE" | |
cp $FILE /var/www-backup | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment