Bash Scripting: Difference between revisions

From Leo's Notes
This page was last edited on 24 February 2017, at 19:12.
m Text replacement - "{{Highlight|lang=bash" to "{{highlight|lang=terminal"
m Text replacement - "<code>" to "{{code|"
Line 32: Line 32:


=== die ===
=== die ===
Similar to the <code>die</code> command in PHP which prints out a message before stopping execution.
Similar to the {{code|die</code> command in PHP which prints out a message before stopping execution.


Function:
Function:
Line 59: Line 59:


=== Extracting / Parsing Filenames ===
=== Extracting / Parsing Filenames ===
Use the <code>basename</code> to extract filenames from a path and and <code>dirname</code> to extract the path of a filename.
Use the {{code|basename</code> to extract filenames from a path and and {{code|dirname</code> to extract the path of a filename.


Eg:
Eg:

Revision as of 19:12, 24 February 2017

String Matching

Regex

Use the new test (ie: x =~ regex ) to test regular expressions.

[[ zone36-ta =~ .*ta$ ]] && echo matched

Or use the new test with wildcards.

[[ zone36-ta =~ *ta ]] && echo matched


Useful Functions / Utilities

Here are some common functions I use in my scripts.

Prompt

You can prompt the user for an yes or no answer using this function. An optional default values are accepted as the second argument.

Example Usage:

if prompt "Is this okay?" Y ; then
    echo "It's Okay!"
 fi
 if ! prompt "Is this okay?" N ; then
    echo "It's not okay!"
 fi

Source: <phorkie>https://phorkie.leo.home.steamr.com/13/embed</phorkie>

die

Similar to the {{code|die command in PHP which prints out a message before stopping execution.

Function:

function die {
	echo $@
	exit
}
Caveat when using in a subshell
This will not work inside a subshell as it will just quit the subshell rather than the script. To get around this, you may need to kill the parent PID instead.


Indent

Indents text piped to the function.

Source: <phorkie>https://phorkie.leo.home.steamr.com/14/embed</phorkie>

Usage:

echo "test" | Indent
	test
echo "test" | Indent 2
		test

Extracting / Parsing Filenames

Use the {{code|basename to extract filenames from a path and and {{code|dirname to extract the path of a filename.

Eg:

$ dirname /etc/hosts
/etc
$ basename /etc/hosts
hosts

To extract only the extension or the filename without the extension, use bash's string matching:

Filename=$(basename /etc/resolv.conf)
Ext="${Filename##*.}"
Name="${Filename%.*}"

Arithmetic Operations

Do integer arithmetic operations on variables within double brackets. For example:

Value=1
ValuePlusOne=$((Value + 1))
ValueTimes12=$((Value * 12))

The operators that are supported are:

+ - / * % 

Bitwise operators also work:

^ (XOR)   | (OR)   & (AND)

Control Blocks

Switch

read -p "What do you want to do?: " Option

case "$Option" in
	"K")
		install_new_kernel
		die "Complete!"
		;;
	"H")
		usage
		;;
	*)
		die "Stopping installation."
		;;
esac

Snippets

Get First IP Address

ip a | /bin/grep inet | grep -v inet6 | tail -n+2 | head -n 1 | awk '{print $2}' | cut -d'/' -f1