bash : arguments : add info about converting args & files to array.
This commit is contained in:
parent
14449bf9ca
commit
df5c4f6e70
|
@ -1,7 +1,27 @@
|
|||
# arguments.
|
||||
## break flags.
|
||||
if you insert \-\- then all input after that will be treated as non-flags.
|
||||
# Arguments.
|
||||
|
||||
## Break flags.
|
||||
|
||||
If you insert \-\- then all input after that will be treated as non-flags.
|
||||
```bash
|
||||
rm -- -r -f # this will remove "-r" and "-f" files instead of recursive and force.
|
||||
```
|
||||
|
||||
## Loop over arguments.
|
||||
|
||||
Handy if you want to loop over files both from arguments or filesystem that include spaces and other special chars. Basically we convert them all to an array and work with it.
|
||||
|
||||
```bash
|
||||
local targets=("${@}") # get array of arguments.
|
||||
|
||||
# if arguments are empty.
|
||||
if [[ "${targets}" = "" ]]; then
|
||||
targets=(*.tar) # select all .tar files by default.
|
||||
count=${#targets[@]} # calculate the array size.
|
||||
fi
|
||||
|
||||
# iterate.
|
||||
for target in "${targets[@]}"; do
|
||||
# something.
|
||||
done
|
||||
```
|
||||
|
|
Loading…
Reference in a new issue