├── 1_cpu-hog ├── 9_processmonitor ├── README.md ├── areyousure ├── array1 ├── backout ├── case ├── cleanuponexit ├── convert ├── countdown ├── debugfile ├── distribute-scripts-and-guides-DEV.sh ├── distribute-scripts-and-guides.sh ├── else1 ├── else2 ├── for ├── funcvar ├── hello-world ├── history-MAY24.txt ├── kube-setup.sh ├── lab5-grade.sh ├── lab53.sh ├── lab97.sh ├── less11_labb ├── loi.sh ├── makechange.sh ├── makeuser ├── mymenu ├── network ├── noargs1 ├── noargs2 ├── numcheck ├── options ├── patternglob ├── pm ├── poem ├── psubst1 ├── psubst2 ├── psubst3 ├── psubst4 ├── read me ├── readit ├── remoteserver.sh ├── remoteserverb.sh ├── removepattern ├── rotating ├── script1 ├── script10 ├── script11 ├── script12 ├── script2 ├── script3 ├── script4 ├── script5 ├── script6 ├── script7 ├── script8 ├── script9 ├── setfalse ├── setup-remote.sh ├── setupftp ├── setupftp-test ├── setupsvc ├── someargs1 ├── someargs2 ├── sudotest.sh ├── tostderr.sh └── trapme /1_cpu-hog: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Script that monitors the top-active process. The script sends an email to the user root if 3 | # utilization of the top active process goed beyond 80%. Of course, this script can be tuned to 4 | # do anything else in such a case. 5 | # 6 | # Start the script, and it will run forever. 7 | 8 | while true 9 | do 10 | # Check every 60 seconds if we have a process causing high CPU load 11 | sleep 60 12 | USAGE=`ps -eo pcpu,pid -o comm= | sort -k1 -n -r | head -1 | awk '{ print $1 } '` 13 | USAGE=${USAGE%.*} 14 | PID=`ps -eo pcpu,pid -o comm= | sort -k1 -n -r | head -1 | awk '{print $2 }'` 15 | PNAME=`ps -eo pcpu,pid -o comm= | sort -k1 -n -r | head -1 | awk '{print $3 }'` 16 | 17 | # Only if we have a high CPU load on one process, run a check within 7 seconds 18 | # In this check, we should monitor if the process is still that active 19 | # If that's the case, root gets a message 20 | if [ $USAGE -gt 80 ] 21 | then 22 | USAGE1=$USAGE 23 | PID1=$PID 24 | PNAME1=$PNAME 25 | sleep 7 26 | USAGE2=`ps -eo pcpu,pid -o comm= | sort -k1 -n -r | head -1 | awk '{ print $1 } '` 27 | USAGE2=${USAGE2%.*} 28 | PID2=`ps -eo pcpu,pid -o comm= | sort -k1 -n -r | head -1 | awk '{print $2 }'` 29 | PNAME2=`ps -eo pcpu,pid -o comm= | sort -k1 -n -r | head -1 | awk '{print $3 }'` 30 | 31 | # Now we have variables with the old process information and with the 32 | # new information 33 | 34 | [ $USAGE2 -gt 80 ] && [ $PID1 = $PID2 ] && mail -s "CPU load of $PNAME is above 80%" root@blah.com < . 35 | fi 36 | done 37 | -------------------------------------------------------------------------------- /9_processmonitor: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # Monitoring process httpd 4 | # 5 | COUNTER=0 6 | while ps aux | grep httpd | grep -v grep > /dev/null 7 | do 8 | COUNTER=$((COUNTER+1)) 9 | sleep 1 10 | echo COUNTER is $COUNTER 11 | done 12 | 13 | logger HTTPMONITOR: httpd stopped at `date` 14 | /etc/init.d/apache2 start 15 | mail -s Aix en panne appelez Bernard mail@sandervanvugt.nl < . 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This Git repository contains supporting files for my Bash Scripting video courses, as well as the "Bash Scripting in 4 Hours" Live course I'm teaching at https://learning.oreilly.com. 2 | -------------------------------------------------------------------------------- /areyousure: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ctrlc_count=0 4 | 5 | function no_ctrlc() 6 | { 7 | let ctrlc_count++ 8 | echo 9 | if [[ $ctrlc_count == 1 ]]; then 10 | echo "Don\'t do that." 11 | elif [[ $ctrlc_count == 2 ]]; then 12 | echo "if you\'re sure, do it once more." 13 | else 14 | echo "Okay, bye." 15 | exit 16 | fi 17 | } 18 | 19 | # define the trap that reacts on ctrl-c 20 | trap no_ctrlc SIGINT 21 | 22 | # run the command on something that will be restarted after receiving SIGINT 23 | # this fails if you do it on while sleep 10000 24 | while true 25 | do 26 | echo doing nothing for a long time 27 | sleep 10000 28 | done 29 | 30 | -------------------------------------------------------------------------------- /array1: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | my_array=( a b c ) 3 | 4 | # print index value 1 5 | echo ${my_array[1]} 6 | 7 | # print all items in the array 8 | echo ${my_array[@]} 9 | echo ${my_array[*]} 10 | 11 | # print all index values and not their value 12 | echo ${!my_array[@]} 13 | 14 | # print the length of the array 15 | echo ${#my_array[@]} 16 | 17 | # loop over all items in the array; printing all keys as well as all values 18 | for i in "${!my_array[@]}" 19 | do 20 | echo "$i" "${my_array[$i]}" 21 | done 22 | 23 | # loop on just the values and not the keys 24 | for i in "${my_array[@]}" 25 | do 26 | echo "$i" 27 | done 28 | 29 | # adding a value at a specific position 30 | # using 9 to make sure it is last 31 | my_array[9]=d 32 | echo ${my_array[@]} 33 | echo ${my_array[9]} 34 | 35 | # adding items to the end of the array, using the first available index 36 | my_array+=( e f ) 37 | for i in "${!my_array[@]}" 38 | do 39 | echo "$i" "${my_array[$i]}" 40 | done 41 | 42 | 43 | -------------------------------------------------------------------------------- /backout: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # backup script that stops if insufficient disk space is available 4 | 5 | if [ -z $1 ] 6 | then 7 | echo enter the name of a directory to back up 8 | read dir 9 | else 10 | dir=$1 11 | fi 12 | 13 | [ -d ${dir}.backup ] || mkdir ${dir}.backup 14 | 15 | for file in $dir/* 16 | do 17 | used=$( df $dir | tail -1 | awk '{ print $5 }' | sed 's/%//' ) 18 | if [ $used -lt 98 ] 19 | then 20 | echo stopping: low disk space 21 | break 22 | fi 23 | 24 | cp $file ${dir}.backup 25 | done 26 | -------------------------------------------------------------------------------- /case: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | echo are you good? 4 | 5 | read GOOD 6 | 7 | GOOD=$(echo $GOOD | tr [:upper:] [:lower:]) 8 | 9 | case $GOOD in 10 | yes,oui) 11 | echo that's nice 12 | ;; 13 | no) 14 | echo that's not so nice 15 | ;; 16 | *) 17 | okay 18 | ;; 19 | esac 20 | -------------------------------------------------------------------------------- /cleanuponexit: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | tempfile=/tmp/tmpdata 3 | touch $tempfile 4 | ls -l $tempfile 5 | trap "rm -f $tempfile" EXIT 6 | 7 | -------------------------------------------------------------------------------- /convert: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # convert file names to lower case if required 4 | 5 | FILES=$(ls) 6 | 7 | for file in $FILES 8 | do 9 | if [[ "$file" != *[[:upper:]]* ]]; then 10 | echo "$file" doesn\'t contain uppercase 11 | continue 12 | fi 13 | 14 | OLD="$file" 15 | NEW=$(echo $file | tr '[:upper:]' '[:lower:]') 16 | 17 | mv "$OLD" "$NEW" 18 | echo "$OLD has been renamed to $NEW" 19 | done 20 | -------------------------------------------------------------------------------- /countdown: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | COUNTER=$1 4 | COUNTER=$(( COUNTER * 60 )) 5 | 6 | while true 7 | do 8 | echo $COUNTER seconds remaining in break 9 | COUNTER=$(( COUNTER - 1 )) 10 | sleep 1 11 | done 12 | -------------------------------------------------------------------------------- /debugfile: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Use FD 15 to capture the debug stream caused by "set -x": 4 | exec 15>/tmp/bash-debug.log 5 | # Tell bash about it (there's nothing special about 15, its arbitrary) 6 | export BASH_XTRACEFD=15 7 | 8 | # turn on debugging: 9 | set -x 10 | 11 | # run some commands: 12 | cd /etc 13 | find 14 | echo "that was it" 15 | 16 | # Close the debugging: 17 | set +x 18 | 19 | # Close the file descriptor 20 | exec 15>&- 21 | 22 | # See what we got: 23 | cat /tmp/bash-debug.log 24 | -------------------------------------------------------------------------------- /distribute-scripts-and-guides-DEV.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # script for internal use by SvV to distribute changes from master kubernetes 3 | # git repo to all git repos that need the same stuff 4 | # to be executed from the "kubernetes" directory 5 | 6 | FILES="Setup\ Guide.pdf Setup\ Guide-AiO.pdf minikube-docker-setup.sh setup-container.sh setup-kubetools-ubuntu.sh" 7 | 8 | echo files is set to $FILES 9 | read 10 | 11 | REPOS="cka ckad devopsinfourweeks kub4h microservices" 12 | for i in $REPOS 13 | do 14 | for j in $FILES 15 | do 16 | echo ln $j $i/ 17 | done 18 | done 19 | 20 | for k in $REPOS 21 | do 22 | cd ../$k 23 | echo git add * 24 | echo git commit -m "automated update" 25 | echo git push 26 | done 27 | -------------------------------------------------------------------------------- /distribute-scripts-and-guides.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # script for internal use by SvV to distribute changes from master kubernetes 3 | # git repo to all git repos that need the same stuff 4 | # to be executed from the "kubernetes" directory 5 | 6 | FILES="SetupGuide.pdf SetupGuideAiO.pdf minikube-docker-setup.sh setup-container.sh setup-kubetools-ubuntu.sh" 7 | REPOS="cka ckad devopsinfourweeks kub4h microservices" 8 | REMOVEME= 9 | 10 | for i in $REPOS 11 | do 12 | for j in $FILES 13 | do 14 | ln $j ../$i/ 15 | done 16 | done 17 | 18 | for k in $REPOS 19 | do 20 | cd ../$k 21 | git add * 22 | git commit -m "automated update of scripts and docs" 23 | git push 24 | done 25 | 26 | -------------------------------------------------------------------------------- /else1: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [ -z $1 ] 4 | then 5 | echo no argument provided please try again 6 | exit 1 7 | fi 8 | 9 | echo you only get here if an argument was provided 10 | -------------------------------------------------------------------------------- /else2: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [ -f $1 ] 4 | then 5 | echo $1 is a file 6 | else 7 | echo $1 is not a file 8 | fi 9 | 10 | -------------------------------------------------------------------------------- /for: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "Hello $1, how are you today" 3 | echo "\$# gives $#" 4 | echo "\$* gives $*" 5 | echo "\$@ gives $@" 6 | echo "\$0 is $0" 7 | 8 | # trying to show every single argument on a separated line 9 | echo showing the interpretation of \$* 10 | for i in "$*" 11 | do 12 | echo $i 13 | done 14 | 15 | echo showing the interpretation of \$@ 16 | for i in "$@" 17 | do 18 | echo $i 19 | done 20 | -------------------------------------------------------------------------------- /funcvar: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | var1=A 3 | 4 | my_function () { 5 | local var2=B 6 | var3=C 7 | echo "inside function: var1: $var1, var2: $var2, var3: $var3" 8 | } 9 | 10 | echo "before runninng function: var1: $var1, var2: $var2, var3: $var3" 11 | 12 | my_function 13 | 14 | echo "after running function: var1: $var1, var2: $var2, var3: $var3" 15 | -------------------------------------------------------------------------------- /hello-world: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # This is some comment 4 | # Here you can put how to use the script 5 | 6 | clear 7 | echo hello world 8 | 9 | exit 0 10 | -------------------------------------------------------------------------------- /history-MAY24.txt: -------------------------------------------------------------------------------- 1 | [student@localhost ~]$ history 2 | 1 sudo dnf install -y git 3 | 2 git clone https://github.com/sandervanvugt/bash-scripting 4 | 3 cd bash-scripting/ 5 | 4 ls 6 | 5 cd .. 7 | 6 git clone https://github.com/sandervanvugt/rhcsa-labs 8 | 7 cd rhcsa-labs/ 9 | 8 ls 10 | 9 vim lab123-grade.sh 11 | 10 cd ../bash-scripting/ 12 | 11 ls 13 | 12 vim countdown 14 | 13 history 15 | 14 cat /etc/os-release 16 | 15 history 17 | 16 vim countdown 18 | 17 echo $PATH 19 | 18 env 20 | 19 history 21 | 20 vim countdown 22 | 21 echo $(( 2 + 2 )) 23 | 22 echo $(( 2 + 2000 )) 24 | 23 echo $(( 2 / 2000 )) 25 | 24 echo $(( 2000 / 3 )) 26 | 25 echo $(( 666 * 3 )) 27 | 26 vim countdown 28 | 27 ls 29 | 28 ls &>/dev/null 30 | 29 echo $ 31 | 30 echo $? 32 | 31 ls guyfytdtf &>/dev/null 33 | 32 echo $? 34 | 33 ls > /dev/null 35 | 34 ls 2> /dev/null 36 | 35 ls uyguyguygfuy 2> /dev/null 37 | 36 ls uyguyguygfuy 38 | 37 true 39 | 38 echo $? 40 | 39 false 41 | 40 echo $ 42 | 41 echo $? 43 | 42 false 44 | 43 echo $? 45 | 44 vim countdown 46 | 45 ./countdown 1 47 | 46 vim countdown 48 | 47 LS 49 | 48 history 50 | 49 vim helloworld.sh 51 | 50 helloworld.sh 52 | 51 echo $PATH 53 | 52 ./helloworld.sh 54 | 53 ls -l 55 | 54 ls -l helloworld.sh 56 | 55 chmod +x helloworld.sh 57 | 56 ./helloworld.sh 58 | 57 vim helloworld.sh 59 | 58 ls &>/dev/null 60 | 59 grep -l '\$&' * 61 | 60 grep -l '\$\&' * 62 | 61 ls 63 | 62 vim script3 64 | 63 COLOR=red 65 | 64 echo COLOR 66 | 65 echo $COLOR 67 | 66 echo $COLOR123 68 | 67 echo ${COLOR}123 69 | 68 vim script3 70 | 69 ./script3 71 | 70 ls -l script* 72 | 71 chmod +x script* 73 | 72 cat script3 74 | 73 ./script3 a b c d e f g h i j k l m 75 | 74 vim script4 76 | 75 history | grep git 77 | 76 vim script4 78 | 77 ./script4 a b c d 79 | 78 ls 80 | 79 ./countdown 12 81 | 80 date +%d-%m-%y 82 | 81 vim script6 83 | 82 ./script6 /usr/bin/blah 84 | 83 vim script6 85 | 84 ./script6 /usr/bin/blah 86 | 85 vim script7 87 | 86 ls 88 | 87 vim script10 89 | 88 ./script10 5 90 | 89 ls ; who 91 | 90 ls ufuyfytfyt ; who 92 | 91 ls && who 93 | 92 ls ytfytdfytdf && who 94 | 93 ls ytfytdfytdf || who 95 | 94 ls || who 96 | 95 man test 97 | 96 [ -f /etc/hosts ] && echo it exists 98 | 97 [ -f /etc/hosts ] || echo it exists 99 | 98 [ -f /etcguyuyguyguy ] || echo it not exists 100 | 99 vim script10 101 | 100 vim script7 102 | 101 vim script8 103 | 102 cut -d : -f 1 /etc/passwd 104 | 103 vim script8 105 | 104 ./script8 student 106 | 105 ./script8 bob 107 | 106 ./script8 108 | 107 vim script9 109 | 108 ./script9 110 | 109 echo $? 111 | 110 ./script9 iuhgiughi 112 | 111 vim script10 113 | 112 ls 114 | 113 grep elif * 115 | 114 vim numcheck 116 | 115 ./numcheck 117 | 116 vim script8 118 | 117 [-f /etc/hosts] && echo hello 119 | 118 [ -f /etc/hosts ] && echo hello 120 | 119 vim tr.sh 121 | 120 chmod +x tr.sh 122 | 121 ./tr.sh 123 | 122 ./tr.sh student 124 | 123 ./tr.sh bob 125 | 124 #tar czvf backup-16-05-2024.tgz /etc 126 | 125 data +%d-%m-%Y 127 | 126 date +%d-%m-%Y 128 | 127 tar czvf backup-$(date +%d-%m-%Y).tgz /etc 129 | 128 ls -lrt 130 | 129 history 131 | 130 uname -r 132 | 131 cd /usr/lib 133 | 132 ls 134 | 133 cd modules 135 | 134 ls 136 | 135 cd $(uname -r) 137 | 136 cd 138 | 137 cd bash-scripting/ 139 | 138 vim tr.sh 140 | 139 ./tr.sh student 141 | 140 ./tr.sh bob 142 | 141 vim tr.sh 143 | 142 ./tr.sh 144 | 143 vim script2 145 | 144 ./script2 146 | 145 pwd 147 | 146 vim script2 148 | 147 source script2 149 | 148 pwd 150 | 149 cd - 151 | 150 . script2 152 | 151 pwd 153 | 152 cd - 154 | 153 ls 155 | 154 vim network 156 | 155 vim sourcing 157 | 156 vim colors 158 | 157 chmod +x sourcing 159 | 158 ./sourcing 160 | 159 vim colors 161 | 160 ./sourcing 162 | 161 cat sourcing 163 | 162 cd 164 | 163 vim .bashrc 165 | 164 ls 166 | 165 cd bash-scripting/ 167 | 166 ls 168 | 167 vim script11 169 | 168 ./script11 1 170 | 169 bash -x script11 1 171 | 170 vim script11 172 | 171 ./script11 1 173 | 172 vim script11 174 | 173 ./script11 1 175 | 174 bash -x ./script11 1 176 | 175 vim script11 177 | 176 bash -x ./script11 1 178 | 177 ls 179 | 178 vim 9_processmonitor 180 | 179 sudo dnf install -y httpd 181 | 180 sudo systemctl start httpd 182 | 181 ps aux | grep http 183 | 182 ps aux | grep bogus 184 | 183 ps aux | grep bogus | grep -v grep 185 | 184 ./9_processmonitor 186 | 185 ls 187 | 186 less 1_cpu-hog 188 | 187 ls -l 1_cpu-hog 189 | 188 echo during break: investigate 1_cpu-hog and tell me what is wrong with it 190 | 189 vim 1_cpu-hog 191 | 190 ./1_cpu-hog 192 | 191 vim 1_cpu-hog 193 | 192 ./1_cpu-hog 194 | 193 vim 1_cpu-hog 195 | 194 ./1_cpu-hog 196 | 195 ls 197 | 196 vim setupftp 198 | 197 sudo ./setupftp 199 | 198 ls -l hosts 200 | 199 vim setupftp 201 | 200 lftp localhost 202 | 201 ls 203 | 202 vim kube-setup.sh 204 | 203 ls 205 | 204 cd .. 206 | 205 git clone https://github.com/sandervanvugt/cka 207 | 206 cd cka 208 | 207 ls 209 | 208 vim setup-container.sh 210 | 209 cd ../rhcsa-labs/ 211 | 210 ls 212 | 211 vim exam-grade.sh 213 | 212 echo -e "\033[1mchecking task 1 results\033[0m" 214 | 213 vim exam-grade.sh 215 | 214 vim exam-task14.sh 216 | 215 vim exam-task13.sh 217 | 216 ./exam-grade.sh 218 | 217 vim exam-grade.sh 219 | 218 history 220 | 219 history | grep git 221 | 220 cd 222 | 221 history 223 | 224 | -------------------------------------------------------------------------------- /kube-setup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # verified on Fedora 31, 33 and Ubuntu LTS 20.04 4 | 5 | echo this script works on Fedora 31, 33 and Ubuntu 20.04 6 | echo it does NOT currently work on Fedora 32 7 | echo it requires the machine where you run it to have 6GB of RAM or more 8 | echo press Enter to continue 9 | read 10 | 11 | ########## 12 | echo ######################################## 13 | echo WARNING 14 | echo ######################################## 15 | echo Nov 2020 - currently this script is NOT supported on Mac OS Big Sur 16 | echo I will communicate here one Apple/VMware have provided updates that make it work again 17 | echo 18 | echo Check the Setup Guide provided in this repository for alternative installations 19 | echo 20 | echo press Enter to continue 21 | read 22 | 23 | # setting MYOS variable 24 | MYOS=$(hostnamectl | awk '/Operating/ { print $3 }') 25 | OSVERSION=$(hostnamectl | awk '/Operating/ { print $4 }') 26 | 27 | egrep '^flags.*(vmx|svm)' /proc/cpuinfo || (echo enable CPU virtualization support and try again && exit 9) 28 | 29 | # debug MYOS variable 30 | echo MYOS is set to $MYOS 31 | 32 | #### Fedora config 33 | if [ $MYOS = "Fedora" ] 34 | then 35 | if [ $OSVERSION = 32 ] 36 | then 37 | echo Fedora 32 is not currently supported 38 | exit 9 39 | fi 40 | 41 | sudo dnf clean all 42 | sudo dnf -y upgrade 43 | 44 | # install KVM software 45 | sudo dnf install @virtualization -y 46 | sudo systemctl enable --now libvirtd 47 | sudo usermod -aG libvirt `id -un` 48 | fi 49 | 50 | ### Ubuntu config 51 | if [ $MYOS = "Ubuntu" ] 52 | then 53 | sudo apt-get update -y 54 | sudo apt-get install -y apt-transport-https curl 55 | sudo apt-get upgrade -y 56 | sudo apt-get install -y qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils 57 | 58 | sudo adduser `id -un` libvirt 59 | sudo adduser `id -un` kvm 60 | fi 61 | 62 | # install kubectl 63 | curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl 64 | chmod +x ./kubectl 65 | sudo mv ./kubectl /usr/local/bin/kubectl 66 | 67 | # install minikube 68 | echo downloading minikube, check version 69 | curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 70 | 71 | sudo chmod +x minikube 72 | sudo mv minikube /usr/local/bin 73 | 74 | # start minikube 75 | minikube start --memory 4096 --vm-driver=kvm2 76 | 77 | echo if this script ends with an error, restart the virtual machine 78 | echo and manually run minikube start --memory 4096 --vm-driver=kvm2 79 | -------------------------------------------------------------------------------- /lab5-grade.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # check if there is an application with the name salesweb and assume it's running enough replicas 4 | if kubectl get deploy | grep "salesweb" >/dev/null 5 | then 6 | echo -e "\033[32m[OK]\033[0m\t\t The deployment salesweb is running" 7 | else 8 | echo -e "\033[31m[FAIL]\033[0m\t\t The deployment salesweb is not running. Did you use \033[1mkubectl create deploy salesweb --replicas=3 --image=nginx:1.23\033[0m to start it?" && exit 3 9 | fi 10 | 11 | # check if there is a service resource running 12 | if kubectl get services | grep "salesweb" >/dev/null 13 | then 14 | echo -e "\033[32m[OK]\033[0m\t\t The service salesweb is available" 15 | else 16 | echo -e "\033[31m[FAIL]\033[0m\t\t I cannot find a Service with the name salesweb. Did you use \033[1mkubectl expose deploy salesweb --port=80\033[0m to start it?" && exit 4 17 | fi 18 | 19 | # check if the minikube ingress is on 20 | if [[ $(minikube addons list | awk '/ingress / { print $6 }') == 'enabled' ]] >/dev/null 21 | then 22 | echo -e "\033[32m[OK]\033[0m\t\t The minikube ingress addon is enabled" 23 | else 24 | echo -e "\033[31m[FAIL]\033[0m\t\t the minikube ingress addon is disabled. Did you use \033[1mminikube addons enable ingress\033[0m to start it?" && exit 4 25 | fi 26 | 27 | # check if there is host name resolving to salesweb.example.com 28 | if nslookup salesweb.example.com &> /dev/null 29 | then 30 | echo -e "\033[32m[OK]\033[0m\t\t I can find salesweb.example.com." 31 | else 32 | echo -e "\033[31m[FAIL]\033[0m\t\t I cannot resolve the hostname salesweb.example.com. It should resolve to the minikube IP address. Did you use \033[1mminikube ip\033[0m to find the minikube IP address and add a line to your local Linux /etc/hosts file that resolves salesweb.example.com to that IP address? It should look like \033[1m192.168.49.2 salesweb.example.com\033[0m" 33 | exit 6 34 | fi 35 | 36 | # check if curl salesweb.example.com is giving a result 37 | if curl -s salesweb.example.com | grep -i 'welcome' &> /dev/null 38 | then 39 | echo -e "\033[32m[OK]\033[0m\t\t Succesfully contacted the name based virtual host provided by Ingress" 40 | else 41 | echo -e "\033[31m[FAIL]\033[0m\t\t The kubernetes ingress resource isn't doing it's work (yet). Give it a few seconds and then try again. If it still doesn't work, check if you used \033[1mkubectl create ingress salesweb --rule=\"salesweb.example.com/=salesweb:80\"\033[0m to create it?" && exit 4 42 | fi 43 | 44 | 45 | # successfull completion 46 | echo 47 | echo -e "\033[32m[CONGRATS]\033[0m\t you have successfully completed this lab, please move on to the next lesson" 48 | echo 49 | -------------------------------------------------------------------------------- /lab53.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | [ -b /dev/sdb ] || (echo no /dev/sdb && exit) 3 | 4 | echo this lab needs full access to /dev/sdb and will corrupt the /dev/sdb1 file system 5 | echo press enter if that\'s OK 6 | read 7 | 8 | umount /dev/sdb1 >/dev/null >/dev/null 9 | umount /dev/sdb2 >/dev/null >/dev/null 10 | 11 | dd if=/dev/zero of=/dev/sdb bs=1M count=100 >/dev/null >/dev/null 12 | parted --script /dev/sdb mklabel msdos mkpart primary 1MiB 200MiB >/dev/null 13 | parted --script /dev/sdb mkpart primary 200MiB 500MiB >/dev/null 14 | 15 | partprobe /dev/sdb >/dev/null 16 | mkfs.ext4 /dev/sdb1 >/dev/null 17 | 18 | dd if=/dev/zero of=/dev/sdb1 bs=1024 count=4 >/dev/null 19 | echo b >/proc/sysrq-trigger 20 | -------------------------------------------------------------------------------- /lab97.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | userdel -rf linda 4 | groupdel -rf linda 5 | groupadd -g 1101 linda 6 | useradd -u 1101 -g 1101 linda 7 | echo password | passwd --stdin 8 | 9 | sed -i 's/linda:x:1101:1101::\/home\/linda:\/bin\/bash/linda:x:1101:::\/home\/linda:\/bin\/bash/' /etc/passwd 10 | -------------------------------------------------------------------------------- /less11_labb: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # use readarray to create the associative names 4 | echo enter names for Janitors from Mon-Sun \(seven names required\) 5 | read name1 name2 name3 name4 name5 name6 name7 6 | 7 | declare -A roster; declare -a order 8 | roster[monday]=$name1; order+=( "monday" ) 9 | roster[tuesday]=$name2; order+=( "tuesday" ) 10 | roster[wednesday]=$name3; order+=( "wednesday" ) 11 | roster[thursday]=$name4; order+=( "thursday" ) 12 | roster[friday]=$name5; order+=( "friday" ) 13 | roster[saturday]=$name6; order+=( "saturday" ) 14 | roster[sunday]=$name7; order+=( "sunday" ) 15 | 16 | # print the names of responsible janitors for each day 17 | for i in "${order[@]}" 18 | do 19 | echo "$i" "${roster[$i]}" 20 | done 21 | -------------------------------------------------------------------------------- /loi.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if test -z $1 4 | then 5 | echo geef een aantal minuten 6 | read COUNTER 7 | else 8 | COUNTER=$1 9 | fi 10 | 11 | COUNTER=$(( COUNTER * 60 )) 12 | 13 | while [ $COUNTER -gt 0 ] 14 | do 15 | echo deze pauze duurt nog $COUNTER seconden 16 | COUNTER=$(( COUNTER - 1 __ 17 | sleep 1 18 | done 19 | 20 | echo de pauze is nu over, we gaan verder 21 | -------------------------------------------------------------------------------- /makechange.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | for i in */intro.md; 4 | do 5 | cp $i $i.bak 6 | sed -i -e 's/Challenge/Challenge Lab/g' $i 7 | sed -i -e 's/Scenario/Lab/g' $i 8 | sed -i -e 's/his challenge/his challenge lab/g' $i 9 | sed -i -e 's/hese challenges/hese challenge labs/g' $i 10 | sed -i -e 's/challenges/challenge labs/g' $i 11 | sed -i -e 's/challenge lab lab/challenge lab/g' $i 12 | sed -i -e 's/his scenario/his lab/g' $i 13 | done 14 | 15 | for j in */index.json 16 | do 17 | sed -i -e 's/challenge@0.8/challenge@0.9/g' $j 18 | sed -i -e 's/Challenge/Challenge Lab/g' $j 19 | sed -i -e 's/Scenario/Lab/g' $j 20 | done 21 | -------------------------------------------------------------------------------- /makeuser: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | #makeusr [-u uid] [-g gid] [-i info] [-h homedir] [-s shell] username 3 | function usage 4 | { 5 | echo ‘usage: makeusr [-u uid] [-g gid] [-i info] [-h homedir] ‘ 6 | echo ‘[-s shell] username 7 | exit 1 8 | } 9 | 10 | function helpmessage 11 | { 12 | echo "makeusr is a script ... " 13 | echo "blablabla" 14 | } 15 | 16 | while getopts "u:g:i:h:s:" opt; do 17 | case $opt in 18 | u ) uid=$OPTARG ;; 19 | g ) gid=$OPTARG ;; 20 | i ) info=$OPTARG ;; 21 | h ) home=$OPTARG ;; 22 | s ) shell=$OPTARG ;; 23 | ? ) helpmessage ;; 24 | * ) usage ;; 25 | esac 26 | shift $(($OPTIND -1)) 27 | done 28 | 29 | if [ -z "$1" ]; then 30 | usage 31 | fi 32 | 33 | if [ -n "$2" ]; then 34 | usage 35 | fi 36 | 37 | if [ -z "$uid" ]; then 38 | uid=500 39 | while cut -d : -f3 /etc/passwd | grep -x $uid 40 | do 41 | uid=$((uid+1)) > /dev/null 42 | done 43 | fi 44 | 45 | if [ -z "$gid" ]; then 46 | gid=$(grep users /etc/group | cut -d: -f3) 47 | fi 48 | 49 | if [ -z "$info" ]; then 50 | echo Provide information about the user. 51 | read info 52 | fi 53 | 54 | if [ -z "$home" ]; then 55 | home=/home/$1 56 | fi 57 | 58 | if [ -z "$shell" ]; then 59 | shell=/bin/bash 60 | fi 61 | 62 | echo $1:x:$uid:$gid:$info:$home:$shell >> /etc/passwd 63 | echo $1:::::::: >> /etc/shadow 64 | mkdir -p $home 65 | chmod 660 $home 66 | chown $1:users $home 67 | passwd $1 68 | -------------------------------------------------------------------------------- /mymenu: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | PS3='Enter your choice: ' 4 | options=("Option 1" "Option 2" "Option 3" "Quit") 5 | 6 | select opt in "${options[@]}" 7 | do 8 | case $opt in 9 | "Option 1") 10 | echo "you have selected option 1" 11 | ;; 12 | "Option 2") 13 | echo "you have selected option 2" 14 | ;; 15 | "Option 3") 16 | echo "you have selected $REPLY with is $opt" 17 | ;; 18 | "Quit") 19 | break 20 | ;; 21 | *) echo "invalid option $REPLY";; 22 | esac 23 | done 24 | -------------------------------------------------------------------------------- /network: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | # 3 | # network Bring up/down networking 4 | # 5 | # chkconfig: 2345 10 90 6 | # description: Activates/Deactivates all network interfaces configured to \ 7 | # start at boot time. 8 | # 9 | ### BEGIN INIT INFO 10 | # Provides: $network 11 | # Should-Start: iptables ip6tables NetworkManager-wait-online NetworkManager $network-pre 12 | # Short-Description: Bring up/down networking 13 | # Description: Bring up/down networking 14 | ### END INIT INFO 15 | 16 | # Source function library. 17 | . /etc/init.d/functions 18 | 19 | if [ ! -f /etc/sysconfig/network ]; then 20 | exit 6 21 | fi 22 | 23 | . /etc/sysconfig/network 24 | 25 | if [ -f /etc/sysconfig/pcmcia ]; then 26 | . /etc/sysconfig/pcmcia 27 | fi 28 | 29 | 30 | # Check that networking is up. 31 | [ "${NETWORKING}" = "no" ] && exit 6 32 | 33 | # if the ip configuration utility isn't around we can't function. 34 | [ -x /sbin/ip ] || exit 1 35 | 36 | 37 | CWD=$(pwd) 38 | cd /etc/sysconfig/network-scripts 39 | 40 | . ./network-functions 41 | 42 | # find all the interfaces besides loopback. 43 | # ignore aliases, alternative configurations, and editor backup files 44 | interfaces=$(ls ifcfg-* | \ 45 | LC_ALL=C sed -e "$__sed_discard_ignored_files" \ 46 | -e '/\(ifcfg-lo$\|:\|ifcfg-.*-range\)/d' \ 47 | -e '{ s/^ifcfg-//g;s/[0-9]/ &/}' | \ 48 | LC_ALL=C sort -k 1,1 -k 2n | \ 49 | LC_ALL=C sed 's/ //') 50 | rc=0 51 | 52 | # See how we were called. 53 | case "$1" in 54 | start) 55 | [ "$EUID" != "0" ] && exit 4 56 | rc=0 57 | # IPv6 hook (pre IPv4 start) 58 | if [ -x /etc/sysconfig/network-scripts/init.ipv6-global ]; then 59 | /etc/sysconfig/network-scripts/init.ipv6-global start pre 60 | fi 61 | 62 | apply_sysctl 63 | 64 | #tell NM to reload its configuration 65 | if [ "$(LANG=C nmcli -t --fields running general status 2>/dev/null)" = "running" ]; then 66 | nmcli connection reload 67 | fi 68 | 69 | # bring up loopback interface 70 | action $"Bringing up loopback interface: " ./ifup ifcfg-lo 71 | 72 | case "$VLAN" in 73 | yes) 74 | if [ ! -d /proc/net/vlan ] && ! modprobe 8021q >/dev/null 2>&1 ; then 75 | net_log $"No 802.1Q VLAN support available in kernel." 76 | fi 77 | ;; 78 | esac 79 | 80 | vlaninterfaces="" 81 | vpninterfaces="" 82 | xdslinterfaces="" 83 | bridgeinterfaces="" 84 | 85 | # bring up all other interfaces configured to come up at boot time 86 | for i in $interfaces; do 87 | unset DEVICE TYPE SLAVE NM_CONTROLLED 88 | eval $(LANG=C grep -F "DEVICE=" ifcfg-$i) 89 | eval $(LANG=C grep -F "TYPE=" ifcfg-$i) 90 | eval $(LANG=C grep -F "SLAVE=" ifcfg-$i) 91 | eval $(LANG=C grep -F "NM_CONTROLLED=" ifcfg-$i) 92 | 93 | if [ -z "$DEVICE" ] ; then DEVICE="$i"; fi 94 | 95 | if [ "$SLAVE" = "yes" ] && ( ! is_nm_running || is_false $NM_CONTROLLED ) ; then 96 | continue 97 | fi 98 | 99 | if [ "${DEVICE##cipcb}" != "$DEVICE" ] ; then 100 | vpninterfaces="$vpninterfaces $i" 101 | continue 102 | fi 103 | if [ "$TYPE" = "xDSL" -o "$TYPE" = "Modem" ]; then 104 | xdslinterfaces="$xdslinterfaces $i" 105 | continue 106 | fi 107 | 108 | if [ "$TYPE" = "Bridge" ]; then 109 | bridgeinterfaces="$bridgeinterfaces $i" 110 | continue 111 | fi 112 | if [ "$TYPE" = "IPSEC" ] || [ "$TYPE" = "IPIP" ] || [ "$TYPE" = "GRE" ]; then 113 | vpninterfaces="$vpninterfaces $i" 114 | continue 115 | fi 116 | 117 | if [ "${DEVICE%%.*}" != "$DEVICE" -o "${DEVICE##vlan}" != "$DEVICE" ] ; then 118 | vlaninterfaces="$vlaninterfaces $i" 119 | continue 120 | fi 121 | 122 | if LANG=C grep -EL "^ONBOOT=['\"]?[Nn][Oo]['\"]?" ifcfg-$i > /dev/null ; then 123 | # this loads the module, to preserve ordering 124 | is_available $i 125 | continue 126 | fi 127 | action $"Bringing up interface $i: " ./ifup $i boot 128 | [ $? -ne 0 ] && rc=1 129 | done 130 | 131 | # Bring up xDSL and VPN interfaces 132 | for i in $vlaninterfaces $bridgeinterfaces $xdslinterfaces $vpninterfaces ; do 133 | if ! LANG=C grep -EL "^ONBOOT=['\"]?[Nn][Oo]['\"]?" ifcfg-$i >/dev/null 2>&1 ; then 134 | action $"Bringing up interface $i: " ./ifup $i boot 135 | [ $? -ne 0 ] && rc=1 136 | fi 137 | done 138 | 139 | # Add non interface-specific static-routes. 140 | if [ -f /etc/sysconfig/static-routes ]; then 141 | if [ -x /sbin/route ]; then 142 | grep "^any" /etc/sysconfig/static-routes | while read ignore args ; do 143 | /sbin/route add -$args 144 | done 145 | else 146 | net_log $"Legacy static-route support not available: /sbin/route not found" 147 | fi 148 | fi 149 | 150 | # IPv6 hook (post IPv4 start) 151 | if [ -x /etc/sysconfig/network-scripts/init.ipv6-global ]; then 152 | /etc/sysconfig/network-scripts/init.ipv6-global start post 153 | fi 154 | # Run this again to catch any interface-specific actions 155 | apply_sysctl 156 | 157 | touch /var/lock/subsys/network 158 | 159 | [ -n "${NETWORKDELAY}" ] && /bin/sleep ${NETWORKDELAY} 160 | ;; 161 | stop) 162 | [ "$EUID" != "0" ] && exit 4 163 | # Don't shut the network down if root or /usr is on NFS or a network 164 | # block device. 165 | root_fstype=$(gawk '{ if ($1 !~ /^[ \t]*#/ && $2 == "/" && $3 != "rootfs") { print $3; }}' /proc/mounts) 166 | usr_fstype=$(gawk '{ if ($1 !~ /^[ \t]*#/ && $2 == "/usr" && $3 != "rootfs") { print $3; }}' /proc/mounts) 167 | 168 | if [[ "${root_fstype}" == nfs* || "${usr_fstype}" == nfs* ]] || systemctl show --property=RequiredBy -- -.mount usr.mount | grep -q 'remote-fs.target' ; then 169 | net_log $"rootfs or /usr is on network filesystem, leaving network up" 170 | exit 1 171 | fi 172 | 173 | unset root_fstype usr_fstype 174 | 175 | # Don't shut the network down when shutting down the system if configured 176 | # as such in sysconfig 177 | if is_false "$IFDOWN_ON_SHUTDOWN"; then 178 | if systemctl is-system-running | grep -q 'stopping'; then 179 | net_log $"system is shutting down, leaving interfaces up as requested" info 180 | exit 0 181 | fi 182 | fi 183 | 184 | vlaninterfaces="" 185 | vpninterfaces="" 186 | xdslinterfaces="" 187 | bridgeinterfaces="" 188 | remaining="" 189 | rc=0 190 | 191 | # get list of bonding, vpn, and xdsl interfaces 192 | for i in $interfaces; do 193 | unset DEVICE TYPE 194 | eval $(LANG=C grep -F "DEVICE=" ifcfg-$i) 195 | eval $(LANG=C grep -F "TYPE=" ifcfg-$i) 196 | 197 | if [ -z "$DEVICE" ] ; then DEVICE="$i"; fi 198 | 199 | if [ "${DEVICE##cipcb}" != "$DEVICE" ] ; then 200 | vpninterfaces="$vpninterfaces $i" 201 | continue 202 | fi 203 | if [ "$TYPE" = "IPSEC" ] || [ "$TYPE" = "IPIP" ] || [ "$TYPE" = "GRE" ]; then 204 | vpninterfaces="$vpninterfaces $i" 205 | continue 206 | fi 207 | if [ "$TYPE" = "Bridge" ]; then 208 | bridgeinterfaces="$bridgeinterfaces $i" 209 | continue 210 | fi 211 | if [ "$TYPE" = "xDSL" -o "$TYPE" = "Modem" ]; then 212 | xdslinterfaces="$xdslinterfaces $i" 213 | continue 214 | fi 215 | 216 | if [ "${DEVICE%%.*}" != "$DEVICE" -o "${DEVICE##vlan}" != "$DEVICE" ] ; then 217 | vlaninterfaces="$vlaninterfaces $i" 218 | continue 219 | fi 220 | remaining="$remaining $i" 221 | done 222 | 223 | for i in $vpninterfaces $xdslinterfaces $bridgeinterfaces $vlaninterfaces $remaining; do 224 | unset DEVICE TYPE 225 | (. ./ifcfg-$i 226 | if [ -z "$DEVICE" ] ; then DEVICE="$i"; fi 227 | 228 | if ! check_device_down $DEVICE; then 229 | action $"Shutting down interface $i: " ./ifdown $i boot 230 | [ $? -ne 0 ] && rc=1 231 | fi 232 | ) 233 | done 234 | 235 | action $"Shutting down loopback interface: " ./ifdown ifcfg-lo 236 | 237 | sysctl -w net.ipv4.ip_forward=0 > /dev/null 2>&1 238 | 239 | # IPv6 hook (post IPv4 stop) 240 | if [ -x /etc/sysconfig/network-scripts/init.ipv6-global ]; then 241 | /etc/sysconfig/network-scripts/init.ipv6-global stop post 242 | fi 243 | 244 | rm -f /var/lock/subsys/network 245 | ;; 246 | status) 247 | echo $"Configured devices:" 248 | echo lo $interfaces 249 | 250 | echo $"Currently active devices:" 251 | echo $(/sbin/ip -o link show up | awk -F ": " '{ print $2 }') 252 | ;; 253 | restart|force-reload) 254 | cd "$CWD" 255 | $0 stop 256 | $0 start 257 | rc=$? 258 | ;; 259 | *) 260 | echo $"Usage: $0 {start|stop|status|restart|force-reload}" 261 | exit 2 262 | esac 263 | 264 | exit $rc 265 | -------------------------------------------------------------------------------- /noargs1: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # echo this scripts tests -n when no arguments are provided 4 | 5 | [ -n $1 ] && echo no arguments were provided 6 | -------------------------------------------------------------------------------- /noargs2: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # echo this scripts tests -n when no arguments are provided 4 | 5 | [[ -n $1 ]] && echo no arguments were provided 6 | -------------------------------------------------------------------------------- /numcheck: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | echo -n "Enter a number: " 4 | read NUM1 5 | echo -n "Enter another number: " 6 | read NUM2 7 | echo -n "Enter a third number: " 8 | read NUM3 9 | 10 | if [ $NUM1 -ge $NUM2 ] && [ $NUM1 -ge $NUM3 ] 11 | then 12 | echo $NUM1 is the largest number 13 | elif [ $NUM2 -ge $NUM1 ] && [ $NUM2 -ge $NUM3 ] 14 | then 15 | echo $NUM2 is the largest number 16 | else 17 | echo $NUM3 is the largest number 18 | fi 19 | -------------------------------------------------------------------------------- /options: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | while getopts "hs:" arg; do 3 | case $arg in 4 | h) 5 | echo "usage" 6 | ;; 7 | s) 8 | strength=$OPTARG 9 | echo $strength 10 | ;; 11 | esac 12 | done 13 | -------------------------------------------------------------------------------- /patternglob: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | shopt -s extglob 3 | for i in * 4 | do 5 | case $i in 6 | !(*.doc|*.txt|*.pdf)) 7 | echo $i is not a document 8 | ;; 9 | *) 10 | echo $i is a document 11 | ;; 12 | esac 13 | done 14 | -------------------------------------------------------------------------------- /pm: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | VAR=donkey 4 | echo $VAR 5 | 6 | VAR=${VAR/donkey/horse} 7 | echo $VAR 8 | -------------------------------------------------------------------------------- /poem: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # poem.sh: Pretty-prints one of the ABS Guide author's favorite poems. 3 | # credits: TLDP Advanced Bash Scripting Guide 4 | 5 | # Lines of the poem (single stanza). 6 | Line[1]="I do not know which to prefer," 7 | Line[2]="The beauty of inflections" 8 | Line[3]="Or the beauty of innuendoes," 9 | Line[4]="The blackbird whistling" 10 | Line[5]="Or just after." 11 | # Note that quoting permits embedding whitespace. 12 | 13 | # Attribution. 14 | Attrib[1]=" Wallace Stevens" 15 | Attrib[2]="\"Thirteen Ways of Looking at a Blackbird\"" 16 | # This poem is in the Public Domain (copyright expired). 17 | 18 | echo 19 | 20 | tput bold # Bold print. 21 | 22 | for index in 1 2 3 4 5 # Five lines. 23 | do 24 | printf " %s\n" "${Line[index]}" 25 | done 26 | 27 | for index in 1 2 # Two attribution lines. 28 | do 29 | printf " %s\n" "${Attrib[index]}" 30 | done 31 | 32 | tput sgr0 # Reset terminal. 33 | # See 'tput' docs. 34 | 35 | echo 36 | 37 | exit 0 38 | -------------------------------------------------------------------------------- /psubst1: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | echo enter username or press enter to use default value 4 | read username 5 | echo ${username:-$(whoami)} 6 | -------------------------------------------------------------------------------- /psubst2: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo take one 3 | echo ${var:-abc} 4 | echo ${var} 5 | 6 | echo take two 7 | echo ${var:=abc} 8 | echo ${var} 9 | -------------------------------------------------------------------------------- /psubst3: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | echo check for existence of essential variables 4 | 5 | : ${HOSTNAME?} ${USER?} 6 | echo if you see this step 1 worked 7 | 8 | ${COW?} 9 | echo you shouldnt see this 10 | -------------------------------------------------------------------------------- /psubst4: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo ${1-?"Usage: $0 ARGUMENT"} 3 | echo string length of the argument is ${#1} 4 | -------------------------------------------------------------------------------- /read me: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sandervanvugt/bash-scripting/1daf1c140f5fae1ba596e74d8b0eab2aa17ac6a4/read me -------------------------------------------------------------------------------- /readit: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # source 3 | 4 | echo which directory do you want to go to? 5 | 6 | read DIR 7 | 8 | cd $DIR 9 | pwd 10 | ls 11 | 12 | -------------------------------------------------------------------------------- /remoteserver.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # scanning hosts on $NETWORK 4 | echo enter the IP address of the network that you want to scan for available hosts 5 | read NETWORK 6 | 7 | # enabling some debugging so that we see what happens 8 | set -x 9 | hosts=() 10 | # below IFS is set at the same line as the read statement to make sure it affects the read statement only 11 | # IFS is set to a space to make sure that as long as it finds a space after an item the script continues 12 | while IFS= read -r line; do 13 | hosts+=( "$line" ) 14 | done < <( nmap -sn ${NETWORK}/24 | grep ${NETWORK%.*} | awk '{ print $5 }') 15 | set +x 16 | 17 | # the two lines below are for debugging only 18 | echo press enter to continue 19 | read 20 | 21 | # and here we check that the array works as intended 22 | for value in "${hosts[@]}" 23 | do 24 | echo $value 25 | done 26 | -------------------------------------------------------------------------------- /remoteserverb.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # generating SSH key for local user 4 | [ -f /etc/.ssh/id_rsa ] || ssh-keygen 5 | 6 | # scanning hosts on $NETWORK 7 | echo enter the IP address of the network that you want to scan for available hosts 8 | read NETWORK 9 | 10 | # you can fill an array with command output in two ways. The lines below are not as efficient but also work 11 | #hosts=() 12 | #while IFS= read -r line; do 13 | # hosts+=( "$line" ) 14 | #done < <( nmap -sn ${NETWORK}/24 | grep ${NETWORK%.*} | awk '{ print $5 }') 15 | 16 | # alternative notation 17 | mapfile -t hosts < <(nmap -sn ${NETWORK}/24 | grep ${NETWORK%.*} | awk '{ print $5 }') 18 | 19 | # this line shows debug information; useful while developing but can be removed now 20 | for value in "${hosts[@]}" 21 | do 22 | echo $value 23 | done 24 | 25 | PS3='which host do you want to setup? (Ctrl-C to quit) ' 26 | select host in "${hosts[@]}" 27 | do 28 | case $host in 29 | *) 30 | echo you selected $host 31 | set -v 32 | ssh-copy-id root@$host 33 | scp /etc/hosts root@$host:/etc 34 | set +v 35 | echo this is enough for the proof of concept script 36 | ;; 37 | esac 38 | 39 | done 40 | 41 | -------------------------------------------------------------------------------- /removepattern: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | shopt -s extglob 3 | for i in * 4 | do 5 | echo ${i%%*(.doc|.txt|.pdf)} 6 | done 7 | -------------------------------------------------------------------------------- /rotating: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | sleep 5 & # runs as background process 4 | pid=$! # stores the PID of the most recent background process 5 | 6 | frames="/ | \ \ -" 7 | 8 | while kill -0 $pid 2&>1 ? /dev/null; # kill -0 is a "dry run" and checks if $pid could be killed; this is done for 5 seconds 9 | do 10 | for frame in $frames; # this will split $frames in each of its elements, considering one element in each iteration of the for loop 11 | do 12 | printf "\r$frame Loading..." # prints a carriage return (and no newline!) followed by the value of $frame 13 | sleep 0.5 # this ensures that after 0.5 seconds a new element is displayed 14 | done 15 | done 16 | printf "\n" # prints a new line at the end of the script 17 | -------------------------------------------------------------------------------- /script1: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # This is some comment 4 | # Here you can put how to use the script 5 | 6 | clear 7 | echo hello world 8 | 9 | exit 0 10 | -------------------------------------------------------------------------------- /script10: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | COUNTER=$1 4 | COUNTER=$(( COUNTER * 60 )) 5 | 6 | minusone(){ 7 | COUNTER=$(( COUNTER - 1 )) 8 | sleep 1 9 | } 10 | 11 | while [ $COUNTER -gt 0 ] 12 | do 13 | echo you still have $COUNTER seconds left 14 | minusone 15 | done 16 | 17 | [ $COUNTER = 0 ] && echo time is up && minusone 18 | [ $COUNTER = "-1" ] && echo you now are one second late && minusone 19 | 20 | while true 21 | do 22 | echo you now are ${COUNTER#-} seconds late 23 | minusone 24 | done 25 | -------------------------------------------------------------------------------- /script11: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | COUNTER=$1 4 | COUNTER=$(( COUNTER * 60 )) 5 | 6 | minusone()({ 7 | COUNNTER=$(( COUNTER - 1 )) 8 | sleep 1 9 | } 10 | 11 | while [ $COUNTER -gt 0 ] 12 | do 13 | echo you still have $COUNTER seconds left 14 | minusonne 15 | done 16 | 17 | [ $COUNTER = 0 ] && echo time is up && minusone 18 | [ $COUNTER = "-1" ] && echo you now are one second late && minusone 19 | 20 | while true 21 | do 22 | echo you now are ${COUNTER#-} seconds late 23 | minusone 24 | done 25 | -------------------------------------------------------------------------------- /script12: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | echo are you good? 4 | 5 | read GOOD 6 | 7 | GOOD=$(echo $GOOD | tr [:upper:] [:lower:]) 8 | 9 | case $GOOD in 10 | yes,oui) 11 | echo that's nice 12 | ;; 13 | no) 14 | echo that's not so nice 15 | ;; 16 | *) 17 | okay 18 | ;; 19 | esac 20 | -------------------------------------------------------------------------------- /script2: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # source 3 | 4 | echo which directory do you want to go to? 5 | 6 | read DIR 7 | 8 | cd ${DIR} 9 | pwd 10 | ls 11 | 12 | -------------------------------------------------------------------------------- /script3: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # ... 4 | 5 | echo "Hello $1, how are you today" 6 | echo " hello $2, how are you" 7 | echo " hello $10, how are you" 8 | echo " hello ${10}" 9 | echo " hello ${11}" 10 | shift 11 | echo hi $1 12 | echo "\$0 is $0" 13 | -------------------------------------------------------------------------------- /script4: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "Hello $1, how are you today" 3 | echo "\$# gives $#" 4 | echo "\$* gives $*" 5 | echo "\$@ gives $@" 6 | echo "\$0 is $0" 7 | 8 | # trying to show every single argument on a separated line 9 | echo showing the interpretation of \$* 10 | for i in "$*" 11 | do 12 | echo $i 13 | done 14 | 15 | echo showing the interpretation of \$@ 16 | for i in "$@" 17 | do 18 | echo $i 19 | done 20 | -------------------------------------------------------------------------------- /script5: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | 4 | echo enter the full path name of the file that you want to copy 5 | read file 6 | cp $file /lib/modules/`uname -r` 7 | 8 | -------------------------------------------------------------------------------- /script6: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # ... 4 | # to test, use /usr/bin/blah 5 | 6 | filename=${1##*/} 7 | echo 'filename=${1##*/}' 8 | echo "The name of the file is $filename" 9 | directoryname=${1%/*} 10 | echo 'directoryname=${1%/*}' 11 | echo "the name of the directory is $directoryname" 12 | -------------------------------------------------------------------------------- /script7: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | BLAH=rababarabarabarara 3 | clear 4 | 5 | echo BLAH=$BLAH 6 | echo 'the result of ##*ba is' ${BLAH##*ba} 7 | echo 'the result of #*ba is' ${BLAH#*ba} 8 | echo 'the result of %%ba* is' ${BLAH%%ba*} 9 | echo 'the result of %ba* is' ${BLAH%ba*} 10 | -------------------------------------------------------------------------------- /script8: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | if cut -d : -f 1 /etc/passwd | grep ^$1$ > /dev/null 3 | then 4 | echo $1 exists 5 | else 6 | echo $1 does not exist 7 | fi 8 | -------------------------------------------------------------------------------- /script9: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | if [ -z $1 ] 3 | # if test -z $1 4 | then 5 | echo you have to provide an argument with this script 6 | exit 1 7 | fi 8 | 9 | echo the argument is $1 10 | -------------------------------------------------------------------------------- /setfalse: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | false 6 | echo do we see this 7 | -------------------------------------------------------------------------------- /setup-remote.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # generating SSH key for local user 4 | [ -f $HOME/.ssh/id_rsa ] || ssh-keygen 5 | 6 | # scanning hosts on $NETWORK 7 | echo enter the IP address of the network that you want to scan for available hosts 8 | read NETWORK 9 | 10 | # you can fill an array with command output in two ways. The lines below are not as efficient but also work 11 | #hosts=() 12 | #while IFS= read -r line; do 13 | # hosts+=( "$line" ) 14 | #done < <( nmap -sn ${NETWORK}/24 | grep ${NETWORK%.*} | awk '{ print $5 }') 15 | 16 | # alternative notation 17 | mapfile -t hosts < <(nmap -sn ${NETWORK}/24 | grep ${NETWORK%.*} | awk '{ print $5 }') 18 | 19 | # this line shows debug information; useful while developing but can be removed now 20 | for value in "${hosts[@]}" 21 | do 22 | echo $value 23 | done 24 | 25 | PS3='which host do you want to setup? (Ctrl-C to quit) ' 26 | select host in "${hosts[@]}" 27 | do 28 | case $host in 29 | *) 30 | echo you selected $host 31 | set -v 32 | ssh-copy-id root@$host 33 | scp /etc/hosts root@$host:/etc 34 | set +v 35 | echo this is enough for the proof of concept script 36 | ;; 37 | esac 38 | 39 | done 40 | 41 | -------------------------------------------------------------------------------- /setupftp: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # due to the location in the course where this script is used it lacks the proper conditional checks 4 | sudo yum install -y vsftpd lftp 5 | sudo sed -i -e 's/anonymous_enable=NO/anonymous_enable=YES/' /etc/vsftpd/vsftpd.conf 6 | 7 | # again this is not pretty but I don't want to show anything that hasn't been covered yet 8 | sudo systemctl disable --now vsftpd 9 | sudo systemctl enable --now vsftpd 10 | sudo cp /etc/hosts /var/ftp/pub/ 11 | 12 | # fetching a file wit a here document 13 | lftp localhost < /dev/null, and it will not show the >&2 line 6 | 7 | echo "start" >&2 8 | echo "continuing" &>2 9 | echo "this is an error" 10 | echo "end" 11 | 12 | -------------------------------------------------------------------------------- /trapme: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | trap "echo ignoring signal" SIGINT SIGTERM 3 | echo pid is $$ 4 | 5 | while : 6 | do 7 | sleep 60 8 | done 9 | --------------------------------------------------------------------------------