Check existence of input argument in a Bash shell script

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.

Syntax error near unexpected token ‘then’

There must be a space between if and [, like this: These (and their combinations) would all be incorrect too: These on the other hand are all ok: Btw these are equivalent: These are also equivalent: And, the middle part of your script would have been better with an elif like this: (I also dropped …

Read more

Run script on mac prompt “Permission denied”

Please read the whole answer before attempting to run with sudo Try running sudo /dvtcolorconvert.rb ~/Themes/ObsidianCode.xccolortheme The sudo command executes the commands which follow it with ‘superuser’ or ‘root’ privileges. This should allow you to execute almost anything from the command line. That said, DON’T DO THIS! If you are running a script on your …

Read more

What does set -e mean in a bash script?

From help set : But it’s considered bad practice by some (bash FAQ and irc freenode #bash FAQ authors). It’s recommended to use: to run do_something function when errors occur. See http://mywiki.wooledge.org/BashFAQ/105

What does set -e mean in a bash script?

From help set : But it’s considered bad practice by some (bash FAQ and irc freenode #bash FAQ authors). It’s recommended to use: to run do_something function when errors occur. See http://mywiki.wooledge.org/BashFAQ/105

How to sort an array in Bash

You don’t really need all that much code: Supports whitespace in elements (as long as it’s not a newline), and works in Bash 3.x. e.g.: Note: @sorontar has pointed out that care is required if elements contain wildcards such as * or ?: The sorted=($(…)) part is using the “split and glob” operator. You should …

Read more

zip error – Nothing to do

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

What’s the meaning of the parameter -e for bash shell command line?

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