How to compare strings in Bash

Using variables in if statements If you want to do something when they don’t match, replace = with !=. You can read more about string operations and arithmetic operations in their respective documentation. Why do we use quotes around $x? You want the quotes around $x, because if it is empty, your Bash script encounters … Read more

Read a file line by line assigning the value to a variable

The following reads a file passed as an argument line by line: This is the standard form for reading lines from a file in a loop. Explanation: IFS= (or IFS=”) prevents leading/trailing whitespace from being trimmed. -r prevents backslash escapes from being interpreted. Or you can put it in a bash file helper script, example … Read more

Ubuntu says “bash: ./program Permission denied” [closed]

chmod u+x program_name. Then execute it. If that does not work, copy the program from the USB device to a native volume on the system. Then chmod u+x program_name on the local copy and execute that. Unix and Unix-like systems generally will not execute a program unless it is marked with permission to execute. The … Read more

What is the preferred Bash shebang?

You should use #!/usr/bin/env bash for portability: different *nixes put bash in different places, and using /usr/bin/env is a workaround to run the first bash found on the PATH. And sh is not bash.

In the shell, what does ” 2>&1 ” mean?

File descriptor 1 is the standard output (stdout).File descriptor 2 is the standard error (stderr). Here is one way to remember this construct (although it is not entirely accurate): at first, 2>1 may look like a good way to redirect stderr to stdout. However, it will actually be interpreted as “redirect stderr to a file … Read more

How to create a file in Linux from terminal window? [closed]

Depending on what you want the file to contain: touch /path/to/file for an empty file somecommand > /path/to/file for a file containing the output of some command. eg: grep –help > randomtext.txt echo “This is some text” > randomtext.txt nano /path/to/file or vi /path/to/file (or any other editor emacs,gedit etc)It either opens the existing one for editing or creates & opens … Read more

How to split a string into an array in Bash?

Note that the characters in $IFS are treated individually as separators so that in this case fields may be separated by either a comma or a space rather than the sequence of the two characters. Interestingly though, empty fields aren’t created when comma-space appears in the input because the space is treated specially. To access … Read more

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