Understanding I/O

The numbers are file descriptors and only the first three (starting with zero) have a standardized meaning:




So each of these numbers in your command refer to a file descriptor. You can either redirect a file descriptor to a file with>or redirect it to another file descriptor with>&

The3>&1in your command line will create a new file descriptor and redirect it to1which isSTDOUT. Now1>&2will redirect the file descriptor 1 toSTDERRand2>&3will redirect file descriptor 2 to 3 which isSTDOUT.

So basically you switchedSTDOUTandSTDERR, these are the steps:

  1. Create a new fd 3 and point it to the fd 1
  2. Redirect file descriptor 1 to file descriptor 2. If we wouldn't have saved the file descriptor in 3 we would lose the target.
  3. Redirect file descriptor 2 to file descriptor 3. Now file descriptors one and two are switched.

Now if the program prints something to the file descriptor 1, it will be printed to the file descriptor 2 and vice versa.

Extracting relevant data from logs

Let's imagine our web server is receiving a large amount of queries and this is affecting teh performance.

We can display the ip which are performing more queries with this command:

cat access.log | awk '{print $1}' | sort -n | uniq -c | sort -nr | head -20

Find and rename folders/files given a regex

Log parsing in a folder with logs 2015/20/10/10/300

what is command1 && command2

what is command1 || command2

$$ - Pid Process

bash4$ echo $$
bash4$ echo $BASHPID
$! is PID of last job running in background

$* expands to a single argument with all the elements delimited by spaces (actually the first character of $IFS).

[email protected] expands to multiple arguments.

echo "With *:"
for arg in "$*"; do echo "<$arg>"; done
echo "With @:"
for arg in "[email protected]"; do echo "<$arg>"; done

Operator Precedence

if [ -n "${10}" ]  # Parameters > $9 must be enclosed in {brackets}.
 echo "Parameter #10 is ${10}"
Variable    Use
$#    Stores the number of command-line arguments that were passed to the shell program.
$?    Stores the exit value of the last command that was executed.
$0    Stores the first word of the entered command (the name of the shell program).
$*    Stores all the arguments that were entered on the command line ($1 $2 ...).
"[email protected]"    Stores all the arguments that were entered on the command line, individually quoted ("$1" "$2" ...).



echo "[email protected]"    # 1 2 3 4 5

Reverse a String arguments (ex: Hello world prints world hello - take in consideration spaces

Command to create an empty file


results matching ""

    No results matching ""