additions to bash vars and arrays

This commit is contained in:
thomasabishop 2023-05-05 10:23:45 +01:00
parent af3e4f2861
commit a71a362507
2 changed files with 60 additions and 7 deletions

View file

@ -69,17 +69,48 @@ echo preExistingArray
> Note that we have to put the item we want to addend within array brackets > 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 if [ -z "${another_array[@]}" ]; then
for x in ./l*.sh; do echo "Array is empty"
echo -n "$x " else
done echo "Array is not empty"
echo 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 ## 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. With Bash 4 we gained an additional array-like data structure that is key-value based and similar to maps in other languages.

View file

@ -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 - 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 ## Parameter expansion
// TODO: What is the difference betweeen `$var`, `${var}` and `"${var}"` ? // TODO: What is the difference betweeen `$var`, `${var}` and `"${var}"` ?