Bash cheat sheet

From Coolscript
Revision as of 18:28, 28 October 2021 by Admin (talk | contribs) (Created page with "*Getoptions with flags while getopts "f" option; do case ${option} in f) flag="1" *) echo "unknown parameter" exit;; esac done *Getoptions wi...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
  • 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 -OR-
if "$isA" || "$isB"; then
 do-something
fi 
  • Condition if var is defined
if [ ${var+x} ]; then 
 do-something
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
"