What does $@ mean in a shell script?

$@ is all of the parameters passed to the script. For instance, if you call ./someScript.sh foo bar then $@ will be equal to foo bar. If you do: ./someScript.sh foo bar and then inside someScript.sh reference: umbrella_corp_options “$@” this will be passed to umbrella_corp_options with each individual parameter enclosed in double quotes, allowing to … Read more

Check if a file exists with a wildcard in a shell script [duplicate]

For Bash scripts, the most direct and performant approach is: if compgen -G “${PROJECT_DIR}/*.png” > /dev/null; then echo “pattern exists!” fi This will work very speedily even in directories with millions of files and does not involve a new subshell. Source The simplest should be to rely on ls return value (it returns non-zero when … Read more

How can I have a newline in a string in sh?

If you’re using Bash, the solution is to use $’string’, for example: STR=$’Hello\nWorld’ echo “$STR” # quotes are required here! Prints: Hello World If you’re using pretty much any other shell, just insert the newline as-is in the string: STR=’Hello World’ Bash recognizes a number of other backslash escapes characters in the $” string. Here … Read more

How to execute mongo commands through shell scripts?

You can also evaluate a command using the –eval flag, if it is just a single command. mongo –eval “printjson(db.serverStatus())” Please note: if you are using Mongo operators, starting with a $ sign, you’ll want to surround the eval argument in single quotes to keep the shell from evaluating the operator as an environment variable: … Read more

How can I split a shell command over multiple lines when using an IF statement?

The line-continuation will fail if you have whitespace (spaces or tab characters¹) after the backslash and before the newline. With no such whitespace, your example works fine for me: $ cat test.sh if ! fab –fabfile=.deploy/fabfile.py \ –forward-agent \ –disable-known-hosts deploy:$target; then echo failed else echo succeeded fi $ alias fab=true; . ./test.sh succeeded $ … Read more

“echo -n” prints “-n”

There are multiple versions of the echo command, with different behaviors. Apparently the shell used for your script uses a version that doesn’t recognize -n. The printf command has much more consistent behavior. echo is fine for simple things like echo hello, but I suggest using printf for anything more complicated. What system are you … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)