Skip to content
bobby_dreamer

Unix Shell Learnings

shell, unix1 min read

Created on: 2021-07-26

Creating multiple files and folders in single command

1-- Creates multiple subfolders(test1, test2) under ./lib folder
2 mkdir -p routes/{books,homepages,number,posts,user,wiki}
3
4-- Creates files package.json, index.js in the subfolders lib/test1 and lib/test2
5 touch routes/{books,homepages,number,posts,user,wiki}/{index.js,README.md}

/dev/null 2>&1

Source : What Does > /Dev/Null 2>&1 Mean?

Output redirection

The greater-thans (>) in commands like these redirect the program’s output somewhere. In this case, something is being redirected into /dev/null, and something is being redirected into &1.

Standard in, out, and error

There are three standard sources of input and output for a program. Standard input usually comes from the keyboard if it’s an interactive program, or from another program if it’s processing the other program’s output. The program usually prints to standard output, and sometimes prints to standard error. These three file descriptors (you can think of them as “data pipes”) are often called STDIN, STDOUT, and STDERR.

Sometimes they’re not named, they’re numbered! The built-in numberings for them are 0, 1, and 2, in that order. By default, if you don’t name or number one explicitly, you’re talking about STDOUT.

Given that context, you can see the command above is redirecting standard output into /dev/null, which is a place you can dump anything you don’t want (often called the bit-bucket), then redirecting standard error into standard output (you have to put an & in front of the destination when you do this).

The short explanation, therefore, is “all output from this command should be shoved into a black hole.” That’s one good way to make a program be really quiet!


Return value to a variable

1Sushanth@Sushanth-VAIO MINGW64 /d/GITs/prehook (master)
2$ git status
3On branch master
4Changes to be committed:
5 (use "git restore --staged <file>..." to unstage)
6 new file: "\340\256\207\340\256\250\340\257\215\340\256\244\340\256\277\340\256\257\340\256\276.txt"
7
8
9Sushanth@Sushanth-VAIO MINGW64 /d/GITs/prehook (master)
10$ rc=$(git status)
11
12Sushanth@Sushanth-VAIO MINGW64 /d/GITs/prehook (master)
13$ echo $rc
14On branch master Changes to be committed: (use "git restore --staged <file>..." to unstage) new file: "\340\256\207\340\256\250\340\257\215\340\256\244\340\256\277\340\256\257\340\256\276.txt"

Reading a file to a variable

1# $COMMIT_MSG_FILE has the file path
2 e_msg=$(cat "$COMMIT_MSG_FILE")
3
4# Substring
5 echo "${e_msg:0:5}"

wc command

wc is word count and its used for counting purpose.

1Sushanth@Sushanth-VAIO MINGW64 /d/GITs/prehook (master)
2$ echo $rc
3On branch master Changes to be committed: (use "git restore --staged <file>..." to unstage) new file: "\340\256\207\340\256\250\340\257\215\340\256\244\340\256\277\340\256\257\340\256\276.txt"
4
5Sushanth@Sushanth-VAIO MINGW64 /d/GITs/prehook (master)
6$ echo $rc | wc
7 1 17 193

Simplest options available are

  • -l : Returns how many lines
  • -w : Returns how many words
  • -c : Returns how many characters
1Sushanth@Sushanth-VAIO MINGW64 /d/GITs/prehook (master)
2$ echo $rc | wc -l
31
4
5Sushanth@Sushanth-VAIO MINGW64 /d/GITs/prehook (master)
6$ echo $rc | wc -w
717
8
9Sushanth@Sushanth-VAIO MINGW64 /d/GITs/prehook (master)
10$ echo $rc | wc -c
11193

'LC_ALL' variable

The LCALL variable sets all locale variables output by the command 'locale -a'. It is a convenient way of specifying a language environment with one variable, without having to specify each LC* variable. Processes launched in that environment will run in the specified locale.

The value 'LC_ALL=C' is essentially an English-only environment that specifies the ANSI C locale.

Some language setting for LC_ALL are "ja" for Japanese and "us" for US English. For example, 'LC_ALL=ja'.


Convert String to Array

1string=$(git diff --cached --name-only --diff-filter=A -z HEAD | sed 's/\x0/ /g')
2
3## String to array
4IFS=' ' read -r -a array <<< "$string"

For-Loop and If-condition

1for i in "${array[@]}"
2do
3 if !([[ "$i" == I* ]] || [[ "$i" == B* ]]); then
4 echo "pre-commit check : FAIL : First character should be I or B - $i"
5 flag=1
6 fi
7done
8
9if [ "$flag" == 1 ]; then
10 exit 1
11else
12 echo "pre-commit check : PASS : First character naming : OK"
13fi

-z flag

Checks if the string is empty

1if [[ -z "$COMMIT_SOURCE" ]]; then
2 echo "prepare-commit-msg check : FAIL : No commit message"
3fi

Learning when learning comes