Alex
Alex

Reputation: 35

What does -a "${1%_something}" do in bash

I am trying to understand this bash script. There are some lines of code that I can't explain:

the first one is the if condition on line 4:

if [ ! -d '/var/lib/mysql/mysql' -a "${1%_safe}" = 'mysqld' ]; then

Could someone please explain me, what does the expression -a "${1%_safe}" = 'mysqld' do?

Next on the line 38:

set -- "$@" --init-file="$TEMP_FILE"

What does this statement?

Thanks in advance for all the help!

Upvotes: 0

Views: 65

Answers (1)

Tom Fenech
Tom Fenech

Reputation: 74685

It removes the suffix _safe from the first argument passed to the script $1 and compares it to mysqld. If the suffix is not present, it removes nothing (but still performs the comparison).

See the section on substring removal on this page for more details on string manipulation in Bash.

The -a is used to perform a logical AND between the two conditions inside the test/[ command. Often, people prefer not to do this, especially when the separate conditions are complex, instead opting to use if [ first condition ] && [ second condition ]. As chepner points out in the comments, the -a switch is discouraged by the POSIX standard.

set is a Bash built-in command, which allows you to modify options in the current shell. In many shell commands, -- is used to separate options from arguments, so this runs set with no options and passes "$@" (the arguments passed to the script) and an additional argument --init-file="$TEMP_FILE".

Upvotes: 4

Related Questions