Associative arrays in Shell scripts
To add to Irfan’s answer, here is a shorter and faster version of get() since it requires no iteration over the map contents:
To add to Irfan’s answer, here is a shorter and faster version of get() since it requires no iteration over the map contents:
It is: The $# variable will tell you the number of input arguments the script was passed. Or you can check if an argument is an empty string or not like: The -z switch will test if the expansion of “$1” is a null string or not. If it is a null string then the body is executed.
If your $VARIABLE is a string containing spaces or other special characters, and single square brackets are used (which is a shortcut for the test command), then the string may be split out into multiple words. Each of these is treated as a separate argument. So that one variable is split out into many arguments: The same will be true for any … Read more
Using existing file: Using stdin: Edit: With some paste implementations you need to be more explicit when reading from stdin: <cmd> | paste -sd+ – | bc Options used: -s (serial) – merges all the lines into a single line -d – use a non-default delimiter (the character + in this case)
Answer POSIX compatible: Example use: For Bash specific environments: Explanation Avoid which. Not only is it an external process you’re launching for doing very little (meaning builtins like hash, type or command are way cheaper), you can also rely on the builtins to actually do what you want, while the effects of external commands can easily vary from system to system. … Read more
First, it’s usually better to be explicit about your intent. So if you know the string ends in .rtf, and you want to remove that .rtf, you can just use var2=${var%.rtf}. One potentially-useful aspect of this approach is that if the string doesn’t end in .rtf, it is not changed at all; var2 will contain … Read more
The issue is that you have not provided a name for the zip-files it will create. This will create separate zipped directories for each of the subfolders tmp tmp_dkjg and tmp_dsf
To write a newline use \n not /n the latter is just a slash and a n
The -e option means “if any pipeline ever ends with a non-zero (‘error’) exit status, terminate the script immediately”. Since grep returns an exit status of 1 when it doesn’t find any match, it can cause -e to terminate the script even when there wasn’t a real “error”. If you want to keep the -e option, but also have a grep command that might validly find no … Read more
Use or or _ is a valid character in identifiers. Dot is not, so the shell tried to interpolate $filepath_newstap. You can use set -u to make the shell exit with an error when you reference an undefined variable.