Last active
October 2, 2016 16:12
-
-
Save adamjstewart/c86cad3efbf20bc5f07a2c470d6b6128 to your computer and use it in GitHub Desktop.
Bash function body delimiters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Bash function bodies can be delimited by curly braces, | |
# but they don't have to be. The following are all valid | |
# function body delimiters: | |
# | |
# arithmetic: ((expression)) | |
# test: [[ expression ]] | |
# subshell: (list) | |
# group: { list } | |
# | |
# Credit for the idea of using non-curly brace delimiters goes to: | |
# http://stackoverflow.com/questions/27801932/ | |
cat << EOF | |
########################################################### | |
# Function using double parentheses (( ... )) | |
# Used for arithmetic expansion and evaluation | |
########################################################### | |
# Print True if number is between 0 and 60, else False | |
EOF | |
# Returns true if $1 is an integer between 0 and 60 | |
valid_minutes() (( | |
0 <= $1 && \ | |
$1 <= 60 | |
)) | |
w=59 | |
echo -n "valid_minutes ($w): " | |
if valid_minutes $w | |
then | |
echo "True" | |
else | |
echo "False" | |
fi | |
x=61 | |
echo -n "valid_minutes ($x): " | |
if valid_minutes $x | |
then | |
echo "True" | |
else | |
echo "False" | |
fi | |
# Works the same way as if it was on a single line | |
y=23 | |
echo -n "single line ($y): " | |
if ((0 <= $y && $y <= 60)) | |
then | |
echo "True" | |
else | |
echo "False" | |
fi | |
# The function itself can be defined on a single line | |
valid_minutes() ((0 <= $1 && $1 <= 60)) | |
z=-3 | |
echo -n "single line ($z): " | |
if valid_minutes $z | |
then | |
echo "True" | |
else | |
echo "False" | |
fi | |
cat << EOF | |
########################################################### | |
# Function using double square brackets [[ ... ]] | |
# Used for testing file types and values (Bash-ism) | |
########################################################### | |
# Print True if file has rwx permissions, else False | |
EOF | |
# Returns true if file has all permissions | |
is_rwx() [[ | |
-r $1 && \ | |
-w $1 && \ | |
-x $1 | |
]] | |
temp1=$(mktemp) | |
chmod u+rwx $temp1 | |
echo -n "rwx------ file: " | |
if is_rwx $temp1 | |
then | |
echo "True" | |
else | |
echo "False" | |
fi | |
temp2=$(mktemp) | |
chmod 500 $temp2 | |
echo -n "r-x------ file: " | |
if is_rwx $temp2 | |
then | |
echo "True" | |
else | |
echo "False" | |
fi | |
cat << EOF | |
########################################################### | |
# Function using single parentheses ( ... ) | |
# Runs commands in a subshell | |
########################################################### | |
# Print first and last element of comma separated list | |
EOF | |
# Input argument | |
list="hello,-7,45,world" | |
echo "input list: $list" | |
# IFS (internal field separator) is used to delimit arrays | |
# Defaults to tab, space, and newline character | |
echo "IFS before: '$IFS'" | |
# Returns first and last element of a comma separated list | |
first_and_last() ( | |
IFS=',' | |
array=($1) | |
echo ${array[0]} ${array[-1]} | |
) | |
echo -n "first_and_last: " | |
first_and_last $list | |
# IFS is unchanged since function ran in a subshell | |
echo "IFS after: '$IFS'" | |
cat << EOF | |
# Find installation directory of Bash | |
EOF | |
echo "pwd before: $PWD" | |
# Returns installation directory of Bash | |
bash_dir() ( | |
cd "$(dirname "$(which bash)")" | |
pwd | |
) | |
echo -n "bash_dir: " | |
bash_dir | |
# PWD is unchanged since function ran in a subshell | |
echo "pwd after: $PWD" | |
cat << EOF | |
########################################################### | |
# Function using curly braces { ... } | |
# Runs commands in a group in the current shell | |
########################################################### | |
# Print first and last element of comma separated list | |
EOF | |
# Input argument | |
list="hello,-7,45,world" | |
echo "input list: $list" | |
echo "IFS before: '$IFS'" | |
# Returns first and last element of a comma separated list | |
first_and_last() { | |
IFS=',' | |
array=($1) | |
echo ${array[0]} ${array[-1]} | |
} | |
echo -n "first_and_last: " | |
first_and_last $list | |
# IFS has changed since function ran in this shell | |
echo "IFS after: '$IFS'" | |
cat << EOF | |
# Find installation directory of Bash | |
EOF | |
echo "pwd before: $PWD" | |
# Returns installation directory of Bash | |
bash_dir() { | |
cd "$(dirname "$(which bash)")" | |
pwd | |
} | |
echo -n "bash_dir: " | |
bash_dir | |
# PWD has changed since function ran in this shell | |
echo "pwd after: $PWD" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment