Bash cheat sheet: Difference between revisions

From Coolscript
Jump to navigation Jump to search
 
(17 intermediate revisions by the same user not shown)
Line 1: Line 1:
*Getoptions with flags
=Getoptions with flags=
  while getopts "f" option; do   
  while getopts "f" option; do   
   case ${option} in
   case ${option} in
Line 11: Line 11:




*Getoptions with args
=Getoptions with args=
  while getopts "a:" option; do   
  while getopts "a:" option; do   
   case ${option} in
   case ${option} in
Line 23: Line 23:




*Condition -OR-
=Condition=
  if "$isA" || "$isB"; then
  if "$isA" || "$isB"; then
   do-something
   do-something
Line 33: Line 33:
  fi
  fi


*Condition by integer
DOCKERBACKUP=0
if (( $DOCKERBACKUP > 0 ));then
  echo "....."
fi
*Condition bool
MATCH=true
if [ "$MATCH" = true ]; then
....
fi


*Print Text
=Print Text=
  cat << EOF
  cat << EOF
  Any ...
  Any ...
Line 42: Line 53:




*Read tokenized configuration file
=Read tokenized configuration file=
  . /etc/os-release
  . /etc/os-release
  echo $VERSION_ID
  echo $VERSION_ID


*Search in file, return bool
=Search in file, return bool=
  if grep -q search-arg "/etc/file.conf"; then
  if grep -q search-arg "/etc/file.conf"; then
   do-something
   do-something
Line 52: Line 63:




*Search and replace
=Search and replace=
  sed -i -r  's/(.*)umask 022(.*)$/\t#Changed\n\tumask 027/gi' /etc/profile
  sed -i -r  's/(.*)umask 022(.*)$/\t#Changed\n\tumask 027/gi' /etc/profile


Line 70: Line 81:




==Cool code sample of a function call using args and the use of the return code==
=Code sample of a function call using args and the use of the return code=


  #!/bin/bash
  #!/bin/bash
Line 79: Line 90:
   myreturnvalue=$?
   myreturnvalue=$?
   #echo "myreturnvalue is "$myreturnvalue
   #echo "myreturnvalue is "$myreturnvalue
   if [ $myreturnvalue == "1" ] ; then   #Check for Debian
   if [ $myreturnvalue == "1" ] ; then
   echo "File exist: ${test_path}/${test_file}"
   echo "File exist: ${test_path}/${test_file}"
   else
   else
   echo "File does NOT exist: ${test_path}/${test_file}"
   echo "File does NOT exist: ${test_path}/${test_file}"
   fi
   fi
  }
}
  function chkFile(){
function chkFile(){
  if [ -f $1  ]; then  #Check for file
  if [ -f $1  ]; then  #Check for file
    return 1
  return 1
  else  
  else  
    return 0
  return 0
  fi
  fi
  }
}
  main "$@"; exit 0
  main "$@"; exit 0
=Code sample of a array (1)=
*Declare
declare -a array=()
*Add data
array+=("Red") 
array+=("Blue")
array+=("Yellow")
*Enum
# get length of an array
arraylength=${#array[@]}
# use for loop to read all values and indexes
for (( i=0; i<${arraylength}; i++ ));
do
  echo "index: $i, value: ${array[$i]}"
done
*Return
index: 0, value: Red
index: 1, value: Blue
index: 2, value: Yellow
=Code sample of a array (2)=
CONTAINER[1]="grafana"
VOLUME[1]="docker_grafana-share"
CONTAINER[2]="influx"
VOLUME[2]="docker_influx-volume"
for t in ${!CONTAINER[@]}; do
  echo "$t -> ${CONTAINER[$t]} -> ${VOLUME[$t]};"
done
=Code sample of a array (3)=
Versions="1.18, 1.19, 1.20, 1.21, 1.22, 1.23, 1.24, 1.25, 1.26, 1.27"
arrVersion=(${Versions//,/ })
=Code sample of a array (4)=
messages=(
    "Hello"
    "World"
)
for t in ${messages[@]};do
  echo $t
done
=Search in file for string=
if ! grep -oP '^\s*net.ipv4.ip_forward=1' "/etc/sysctl.conf"; then
  echo "Add by OpenVPN-Installer Script $DateTime" >> "/etc/sysctl.conf"
  echo "net.ipv4.ip_forward=1" >> "/etc/sysctl.conf"
fi
=Check if a user exist=
if getent passwd ${ovpnuser} > /dev/null 2>&1; then
  echo "User exists  ${ovpnuser}"
fi
=Format Date to variable=
mydate=$(date '+%Y-%m-%d--%H%M')
=String manipulation=
*cut non ascii chars
| tr -d '\n\t\r'
=Regex=
*Simple regex
string="This is Version 22.4 for xy"
if [[ "$string" =~ ^(.*)Version(.*)for ]]; then
  curVersion="${BASH_REMATCH[2]}"
fi

Latest revision as of 17:21, 27 November 2023

Getoptions with flags

while getopts "f" option; do   
 case ${option} in
 f)
     flag="1"
 *)
     echo "unknown parameter"
     exit;;
  esac
done


Getoptions with args

while getopts "a:" option; do   
 case ${option} in
 a)
     arg="${OPTARG}"
 *)
     echo "unknown parameter"
     exit;;
  esac
done


Condition

if "$isA" || "$isB"; then
 do-something
fi 
  • Condition if var is defined
if [ ${var+x} ]; then 
 do-something
fi
  • Condition by integer
DOCKERBACKUP=0
if (( $DOCKERBACKUP > 0 ));then
 echo "....."
fi
  • Condition bool
MATCH=true
if [ "$MATCH" = true ]; then
....
fi

Print Text

cat << EOF
Any ...
text ... 
EOF
}


Read tokenized configuration file

. /etc/os-release
echo $VERSION_ID

Search in file, return bool

if grep -q search-arg "/etc/file.conf"; then
 do-something
fi


Search and replace

sed -i -r  's/(.*)umask 022(.*)$/\t#Changed\n\tumask 027/gi' /etc/profile
  • CRLF within string
scriptmsg+="Hello\n"
scriptmsg+="World\n"
echo -e $scriptmsg
NL=$'\n'
echo -e " Hello${NL}Wolrd"
echo "
Hello
World
"


Code sample of a function call using args and the use of the return code

#!/bin/bash
main() {
 declare test_path='/etc'
 declare test_file='motd'
 chkFile "${test_path}/${test_file}"	
 myreturnvalue=$?
 #echo "myreturnvalue is "$myreturnvalue
 if [ $myreturnvalue == "1" ] ; then  
  echo "File exist: ${test_path}/${test_file}"
 else
  echo "File does NOT exist: ${test_path}/${test_file}"
 fi
}
function chkFile(){
 if [ -f $1  ]; then   #Check for file
  return 1
 else 
  return 0
 fi
}
main "$@"; exit 0

Code sample of a array (1)

  • Declare
declare -a array=()
  • Add data
array+=("Red")  
array+=("Blue")
array+=("Yellow")
  • Enum
# get length of an array
arraylength=${#array[@]}

# use for loop to read all values and indexes
for (( i=0; i<${arraylength}; i++ ));
do
  echo "index: $i, value: ${array[$i]}"
done

  • Return
index: 0, value: Red
index: 1, value: Blue
index: 2, value: Yellow


Code sample of a array (2)

CONTAINER[1]="grafana" 
VOLUME[1]="docker_grafana-share"

CONTAINER[2]="influx"
VOLUME[2]="docker_influx-volume"

for t in ${!CONTAINER[@]}; do
 echo "$t -> ${CONTAINER[$t]} -> ${VOLUME[$t]};"
done

Code sample of a array (3)

Versions="1.18, 1.19, 1.20, 1.21, 1.22, 1.23, 1.24, 1.25, 1.26, 1.27"
arrVersion=(${Versions//,/ })


Code sample of a array (4)

messages=(
   "Hello"
   "World"
)

for t in ${messages[@]};do
 echo $t
done

Search in file for string

if ! grep -oP '^\s*net.ipv4.ip_forward=1' "/etc/sysctl.conf"; then 
 echo "Add by OpenVPN-Installer Script $DateTime" >> "/etc/sysctl.conf"
 echo "net.ipv4.ip_forward=1" >> "/etc/sysctl.conf"
fi

Check if a user exist

if getent passwd ${ovpnuser} > /dev/null 2>&1; then
  echo "User exists  ${ovpnuser}"
fi

Format Date to variable

mydate=$(date '+%Y-%m-%d--%H%M')

String manipulation

  • cut non ascii chars

| tr -d '\n\t\r'


Regex

  • Simple regex
string="This is Version 22.4 for xy"
if "$string" =~ ^(.*)Version(.*)for ; then
 curVersion="${BASH_REMATCH[2]}"
fi