What is the benefit of using $() instead of backticks in shell scripts? [duplicate]

The major one is the ability to nest them, commands within commands, without losing your sanity trying to figure out if some form of escaping will work on the backticks. An example, though somewhat contrived: deps=$(find /dir -name $(ls -1tr 201112[0-9][0-9]*.txt | tail -1l) -print) which will give you a list of all files in … Read more

Compare a string using sh shell

You should use the = operator for string comparison: Sourcesystem=”ABC” if [ “$Sourcesystem” = “XYZ” ]; then echo “Sourcesystem Matched” else echo “Sourcesystem is NOT Matched $Sourcesystem” fi; man test says that you use -z to match for empty strings.

source command not found in sh shell

/bin/sh is usually some other shell trying to mimic The Shell. Many distributions use /bin/bash for sh, it supports source. On Ubuntu, though, /bin/dash is used which does not support source. Most shells use . instead of source. If you cannot edit the script, try to change the shell which runs it.

Using variables inside a bash heredoc

In answer to your first question, there’s no parameter substitution because you’ve put the delimiter in quotes – the bash manual says: The format of here-documents is: <<[-]word here-document delimiter No parameter expansion, command substitution, arithmetic expansion, or pathname expansion is performed on word. If any characters in word are quoted, the delimiter is the … Read more

How to redirect output of an entire shell script within the script itself?

Addressing the question as updated. #…part of script without redirection… { #…part of script with redirection… } > file1 2>file2 # …and others as appropriate… #…residue of script without redirection… The braces ‘{ … }’ provide a unit of I/O redirection. The braces must appear where a command could appear – simplistically, at the start … Read more

Select unique or distinct values from a list in UNIX shell script

You might want to look at the uniq and sort applications. ./yourscript.ksh | sort | uniq (FYI, yes, the sort is necessary in this command line, uniq only strips duplicate lines that are immediately after each other) EDIT: Contrary to what has been posted by Aaron Digulla in relation to uniq‘s commandline options: Given the … Read more

What is the difference between $(command) and `command` in shell programming?

The backticks/gravemarks have been deprecated in favor of $() for command substitution because $() can easily nest within itself as in $(echo foo$(echo bar)). There are other differences such as how backslashes are parsed in the backtick/gravemark version, etc. See BashFAQ/082 for several reasons to always prefer the $(…) syntax. Also see the POSIX spec … Read more

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