diff --git a/Programming_Languages/Shell/Lists_and_arrays.md b/Programming_Languages/Shell/Lists_and_arrays.md index 86cdefa..8934366 100644 --- a/Programming_Languages/Shell/Lists_and_arrays.md +++ b/Programming_Languages/Shell/Lists_and_arrays.md @@ -69,17 +69,48 @@ echo preExistingArray > Note that we have to put the item we want to addend within array brackets -## Looping through file system +## Check if array empty -The following script loops through all files in a directory that begin with `l` and which are of the bash file type (`.sh`) : +```sh +an_array=() -```bash -for x in ./l*.sh; do - echo -n "$x " -done -echo +if [ -z "${another_array[@]}" ]; then + echo "Array is empty" +else + echo "Array is not empty" +fi ``` +Here we pass all the elements of the array to a [test](/Programming_Languages/Shell/Test_values_in_Bash.md) condition which tests for an empty string. + +> NB: This will not immediately work in the context of a function. See below. + +## Weirdness with functions + +When you pass an array as an argument to a [function](/Programming_Languages/Shell/Functions_in_Bash.md) it will not immediately be understood to be an array. + +When we use `$1` to individuate the first function argument this is read as string. So if you parsed an array argument as `$1`, any logic you have in the function will work on the assumption that the argument is a string, not an array. + +To get round this we have to effectively _redeclare_ the argument as an array before running any array logic. We do this through a **nameref** (a reference to a variable). The nameref resolves to the value of the variable it references. This allows you to indirectly manipulate the value of the original variable through the nameref. + +A nameref is created with the `-n` flag. The following function uses this method to check if an array is empty: + +```sh +function array_empty() { + declare -n arr=$1 + if [ ${#arr[@]} -gt 0 ]; then + echo "array is not empty" + else + echo "array is empty" + fi +} + +my_array=() +array_empty "my_array" +``` + +You'll notice when we invoke the function we pass the array as a string, this facilitates the nameref operation in the function. + ## Associational arrays / maps With Bash 4 we gained an additional array-like data structure that is key-value based and similar to maps in other languages. diff --git a/Programming_Languages/Shell/Variables_and_data_types.md b/Programming_Languages/Shell/Variables_and_data_types.md index d276571..b120a1c 100644 --- a/Programming_Languages/Shell/Variables_and_data_types.md +++ b/Programming_Languages/Shell/Variables_and_data_types.md @@ -29,6 +29,28 @@ echo ${boolVar} # true - Quotation marks at declaration are also not strictly necessary however they can help avoid bugs. Also serves as a reminder that every type is basically a string at the end of the day +## Common operations + +### Check if a variable exists + +```sh +if [ -v variable_name ]; then + echo "The variable exists." +else + echo "The variable does not exist." +fi +``` + +### Check if a variable is set (defined) + +```sh +if [ -z "${variable_name+x}" ]; then + echo "The variable does not exist." +else + echo "The variable exists." +fi +``` + ## Parameter expansion // TODO: What is the difference betweeen `$var`, `${var}` and `"${var}"` ?