Bash Scripting

From Leo's Notes
Revision as of 18:09, 31 March 2015 by 136.159.16.240 (talk) (Created page with " == 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. Eg: dirn...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
This page was last edited on 31 March 2015, at 18:09.

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)