Here are some Bourne Shell notes that I use to make scripts.
$ my_array=()
$ element=banana
$ my_array+=($element)
$ echo "${#my_array[@]}"
$ echo "${my_array[0]}"
x=0
while [ "$x" -lt "${#my_array[@]}" ]
do
echo "${my_array[$x]}"
x=$(( $x + 1 ))
done
for i in "${my_array[@]}"
do
echo "$i"
done
$ a=3
$ let a=a+1
$ echo $a
4
$ a=3
$ a=$((a+1))
$ echo $a
4
case $1 in
*.mp3) echo "$1 is a mp3 file";;
*.mkv) echo "$1 is a mkv file";;
*)
echo "$1 is an unknown"
echo "Format.";;
esac
if [ $? -eq 0 ] ; then
for x in 1 2 3
do
echo $x
done
for s in www-0{1..8}
do
echo $s
done
By default if you put, a command result inside a variable, and do a for loop on it, the spaces, tabulations and newlines will be considered as new lines.
Let's see a very useless but concrete example.
$ text="Body screaming fucking bloody mess"
for i in $text; do
echo "$i"
done
Body
screaming
fucking
bloody
mess
The reason is because of the IFS (Internal Field Separator) default value is space, tab and newline.
So, to avoid that, we need do redefine IFS variable, to tell that only new lines should be considered as... new lines...
$ IFS=$'\n'
for i in $text; do
echo "$i"
done
Body screaming fucking bloody mess
if [ -s $1 ]
then
echo "$1 exist"
else
echo "$1 doesn't exist"
fi
if [ ! -n "$var" ]; then
if [ -n "$answer" ] && [ "$answer" != "N" ]; then
echo "OK"
fi
if [ $(ssh user@192.168.1.200 mount | grep -q crypt_backup; echo $?) -eq "0" ] && [ $(ps -ef | grep -q [r]sync; echo $?) -ne "0" ] ; then
echo "OK"
fi
$ FILE="The_Dolphins_-_Demo_-_Anemia.ogg"
$ echo "${FILE%.ogg}.mp3"
The_Dolphins_-_Demo_-_Anemia.mp3
$ FILE="The_Dolphins_-_Demo_-_Anemia.ogg"
$ echo "${FILE#The_Dolphins_-_Demo_-_}"
Anemia.ogg
$ line="secure-policy rule: 1"
$ expr match "$line" '.*: \([0-9]\+\)'
1
$ line=" name: STD01_to_ZYXEL_SSH"
$ expr match "$line" '.*: \([a-zA-Z0-9_]\+\).*'
STD01_to_ZYXEL_SSH
while IFS= read -r line
do
echo "$line"
done < /tmp/a_file.txt
Source : https://wizardzines.com/
$ set -e
$ set -u
$ set -o pipefail
$ set -euo pipefail
while [ $a -gt 0 ]
do
echo "a=$a"
let a=$a-1
done
Contact :