Bash Scripting: Difference between revisions

From Leo's Notes
This page was last edited on 24 February 2017, at 04:23.
string matching using new test
m Text replacement - "{{Highlight|lang=bash" to "{{highlight|lang=terminal"
Line 2: Line 2:
=== Regex ===
=== Regex ===
Use the new test (ie: {{code|[[ x =~ regex ]]}}) to test regular expressions.
Use the new test (ie: {{code|[[ x =~ regex ]]}}) to test regular expressions.
{{Highlight|lang=bash|code=
{{highlight|lang=terminal|code=
[[ zone36-ta =~ .*ta$ ]] && echo matched
[[ zone36-ta =~ .*ta$ ]] && echo matched
}}
}}


Or use the new test with wildcards.
Or use the new test with wildcards.
{{Highlight|lang=bash|code=
{{highlight|lang=terminal|code=
[[ zone36-ta =~ *ta ]] && echo matched
[[ zone36-ta =~ *ta ]] && echo matched
}}
}}
Line 19: Line 19:


Example Usage:
Example Usage:
{{Highlight|lang=bash|code=
{{highlight|lang=terminal|code=
  if prompt "Is this okay?" Y ; then
  if prompt "Is this okay?" Y ; then
     echo "It's Okay!"
     echo "It's Okay!"
Line 51: Line 51:


Usage:
Usage:
{{Highlight|lang=bash|code=
{{highlight|lang=terminal|code=
echo "test" {{!}} Indent
echo "test" {{!}} Indent
test
test
Line 62: Line 62:


Eg:
Eg:
{{Highlight|lang=bash|code=
{{highlight|lang=terminal|code=
$ dirname /etc/hosts
$ dirname /etc/hosts
/etc
/etc
}}
}}


{{Highlight|lang=bash|code=
{{highlight|lang=terminal|code=
$ basename /etc/hosts
$ basename /etc/hosts
hosts
hosts
Line 73: Line 73:


To extract only the extension or the filename without the extension, use bash's string matching:
To extract only the extension or the filename without the extension, use bash's string matching:
{{Highlight|lang=bash|code=
{{highlight|lang=terminal|code=
Filename=$(basename /etc/resolv.conf)
Filename=$(basename /etc/resolv.conf)
Ext="${Filename##*.}"
Ext="${Filename##*.}"
Line 81: Line 81:
== Arithmetic Operations ==
== Arithmetic Operations ==
Do integer arithmetic operations on variables within double brackets. For example:
Do integer arithmetic operations on variables within double brackets. For example:
{{Highlight|lang=bash|code=
{{highlight|lang=terminal|code=
Value=1
Value=1
ValuePlusOne=$((Value + 1))
ValuePlusOne=$((Value + 1))
Line 95: Line 95:
== Control Blocks ==
== Control Blocks ==
=== Switch ===
=== Switch ===
{{Highlight|lang=bash|code=
{{highlight|lang=terminal|code=
read -p "What do you want to do?: " Option
read -p "What do you want to do?: " Option



Revision as of 04:23, 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 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 basename to extract filenames from a path and and 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