Quick Tutorial on Writing a Linux/Unix Bash Script

Getting used to bash is a bit awkward when you come from a background of a verbose programming language (i.e Java , which I love by the way). But,if you do it awhile you get accustomed quickly. On the other hand, if you don't need to write scripts much, remembering the syntax is frustrating. This is a quick tutorial and cheat sheet that I hope you find useful. Even a Java Programmer needs to write Bash scripts sometimes.

Start of Script

Line one contains the location of your bash command proceeded with !# as shown here:

!#/bin/bash

Variable Assignments

Name variables starting with
a-z, A-Z. a-z, A-Z, 0-9
and
_
(underscore). Do not put spaces between the variable name and assignment. Valid variables and assigment statements:

myVar=1 MyVar="X" my_var="John"

Referencing Assigned Variables

When referencing assigned variable, precede the variable name with a
$
sign and wrap variable in quotes. Example:

if [ "$myVar" == "X" ]; then echo "myVar = X" fi

While loop

While loops consist of while with condition, do and ending with done. Example:

i=0 while [ $i -lt 4 ] do echo "i = $i" ((i=i+1)) done

Or put do on the same line as while:

while [ $i -lt 4 ]; do echo "i = $i" done

Brackets around conditions

Brackets
[]
must have a space before and after the open and close bracket.

Example:

[ $i -lt 4 ]

getopts command

The getopts command gets the named command line arguments (ie -d “myvalue”) passed to the script. Variables must be one character alpha only preceed by
-
(dash). Example:
ls -l

If the arguments are flags without values, they can be entered as
-
followed by one or more flags. Example with four flags:
ls -latr

Reference guides for getopts:

Colors and Formatting

The ANSI/VT100 terminals and terminal emulators are not just able to display black and white text ; they can display colors and formatted texts thanks to escape sequences. Those sequences are composed of the Escape character (often represented by “
^[
” or “
<Esc>
”) followed by some other characters: “
<Esc>[
FormatCode
m
”.

In Bash, the
<Esc>
character can be obtained with the following syntaxes:

  • \e
  • \033
  • \x1B

To echo This text is bold, you would type:

echo -e "\033[1mThis Text is Bold\033[0m

The
-e
enables the escaping to work with
echo
. Without it it would print
\033[1mThis Text is Bold\033[0m
. With it, it prints
This text is bold
.

Reference for Colors and Formatting

https://misc.flogisoft.com/bash/tip_colors_and_formatting

Sample full bash script

This script reads command line variables, checks their validity and either shows help for the ldfndtxt command or calls a java application to find text within a directory that has a large number of files and prints the filenames that match.

#!/bin/bash path="" text="" exts="" help="" stats="" stop1=-1 while getopts "shn:t:p:e:" opt; do case $opt in t) text="$OPTARG" ;; p) path="$OPTARG" ;; e) exts="$OPTARG" ;; h) help="Y" ;; s) stats="Y" ;; n) stop1="$OPTARG" ;; \?) echo "Invalid option -$OPTARG" >&2 ;; esac done if [ "$help" == "Y" ]; then echo echo -e "\033[1mFind Text in files in a large directory (LDFNDTXT)\n" echo -e "\033[0mgrep doesn't work when there are a large number of files to search through." echo -e "This command searches through the files for a given string (-t) and returns the file names that match." echo -e "It will handle directories where there are an enormous number of files.\n" echo -e "\033[1m-t\033[0m - The text to search for (required)\n" echo -e "\033[1m-p\033[0m - The path to search within. defaults to current path \n" echo -e "\033[1m-e\033[0m - The file extensions to search (required) (i.e. for .json, use \"json\") " echo -e "\033[1m \033[0m for multiple files separate with comma. (i.e. \"xml,json\") \n" echo -e "\033[1m-n\033[0m - Limit search to \033[1mn\033[0m matching files. default -1 (all) \n" echo -e "\033[1m-s\033[0m - Show search stats with files searched, percent complete and matches found for every 100 files searched. \n" echo -e "\033[1m-h\033[0m - Help displays this help \n" echo -e "\033[1mExample: \n" echo -e "\033[1mfndtxt -t \"FindMe\" -e \"json,csv\" -sl\033[0m\n" echo -e "Find all files that have the string \"FindMe\" with a file extension of json or csv. " echo -e "Show stats of search (\033[1m-s\033[0m) and stop searching after 1st match (\033[1m-l\033[0m)." else if [ -z "$path" ]; then path="." fi if [ -z "$exts" ]; then exts="" fi if [ -z "$stats" ]; then stats="N" fi if [ -z "$stop1" ]; then stop1=-1 fi fi Hopefully, you find this useful. Cheers!

Comments

Popular posts from this blog

How to Create a Symbolic Link in Unix (aka Shortcut)

How to Customize Login Message in Linux with System Status Info

How to Fix WARNING: REMOTE HOST IDENTIFICATION HAS CHANGE - Host key verification failed.