Shell Commands - Bash

Bash Window

  • Ctrl + a move the cursor to the first character of the current line
  • Ctrl + e move the cursor to the last character of the current line
  • Ctrl + u delete all characters
  • Ctrl + k delete all characters following by the cursor

Programming with Bash

Operation Control Symbols

  • x ; y ; z
  • d & e & f
  • g | h | i & pipe the stream to commands
  • a && b >> /log
    • write content to pipe into a file
  1. # redirect
  2. cat file > copy_file
  3. # append it
  4. cat file >> copy_file
  5. echo 'Hello, World' > file
  • (a ; b) & (c ; d) & Group tasks
  • \ continue input the command
  • <<
  • >> write in command
  • |&
  • ''
  • ""

Task Controls

  • jobs list the tasks that are running.
  • fg ID move the job to the front with specific ID.
  • CTRL + Z/C Stopped Job
  • bg

Stack of the Operation Directives

  • dirs
  • pushd
  • pwd
  • popd

Variables

  • memo=max* ; ls ; echo $memo
  • echo $person ; echo “$person” ; echo ‘$person’ ; echo $person
  • find /job ${Test}clockwise
  • readonly person set the variable ready-only
  • unset person
  • declare +x -a persons
  • -a declare an array variable
  • -f declare a function variable
  • -i declare an int variable
  • -r declare a readonly variable == readonly
  • -x declare it as a global variable == export
  • PATH the path where shell find bins.
  • printenv show the env variables
  • name=('a' 'b' 'c') Array Variable
  • echo ${A[n]} n is a Number
  • echo ${A[@]} and echo ${A[*]} show all elements
  • :- and := and :?

Expressions

  • Arithmetic Expression
  • (())
  • Logic / Condition Expression
  • [[]]
  • String Patterns
  • # ## % %%
  • Operators

Functions

  • [function] function-name() { }

Control Flow

  • If

if test "$a" = "$b"
then
echo “do something!”
elif
then
echo "do cool thing!"
else
echo “do something else!”
fi

  • For in

for loop-index in argument-list
do
# commands here
done

  • For

for loop-index
do
# commands here
done

  • While

while test-command
do
# commands here
done

  • Until

util test-command
do
# commands here
done

  • break | continue
  • case

case test-string in
pattern1)
commands-1
;;
pattern2)
commands-2
;;

esac

Inner commands

  • read User input handling
  • echo Output
  • export Set Global Env. Variable
  • declare Set Variable and its property
  • type Show the related information of a command
  • exec Execute a command or redirect identifier of a file
  • trap Capture signal
    • trap [commands] [singal]
  • kill Kill Process
  • eval Scan / Give value and execute commands
  • getopts Parsing options
  • sed Editor
  • AWK Pattern Process Language

Demo

Automatically ci to the code base.

#!/bin/bash
# use to update dependencies and rebuild & commit

branch_name="$(git symbolic-ref HEAD 2>/dev/null)" ||
branch_name="(unnamed branch)"     # detached HEAD
branch_name=${branch_name##refs/heads/}

echo -e "\033[33m run git pull origin $branch_name \033[0m"
git pull origin $currentBranch
echo -e "\033[33m run tnpm update -d \033[0m"
tnpm update -d
echo -e "\033[33m run tnpm run build \033[0m"
tnpm run build
echo -e "\033[33m run git commit -am 'chore: rebuild' \033[0m"
git commit -am 'chore: Rebuild'
echo -e "\033[33m run git push -u origin $branch_name \033[0m"
git push -u origin $branch_name

Automatically add owner through tnpm

#!/bin/bash

# 编写批量 tnpm owner add 操作
# 定义组件数组
componentsList=("vc-tree-search-select-field" "waterMark" "vc-float-nav" "vc-monaco-editor-field" "vc-grid" "vc-chart-pie" "vc-chart-line" "vc-chart-column" "vc-chart-radar" "vc-card" "vc-iframe" "vc-link-block" "vc-link" "vc-textarea-field" "vc-switch-field" "vc-rate-field" "vc-table-field" "vc-progress" "vc-jsx" "vc-transfer-field" "vc-block" "vc-title" "vc-cascade-date-field" "vc-date-field" "vc-dialog" "vc-popover" "vc-list-container" "vc-pagination" "vc-mention-field" "vc-columns-layout" "vc-checkbox-field" "vc-text-field" "vc-tag" "vc-image-field" "vc-attachment-field" "vc-number-field" "vc-rich-text" "vc-collapse" "vc-markdown" "vc-matrix" "vc-code-diff" "vc-form" "vc-search-field" "vc-button-group" "vc-page-header" "vc-root" "vc-section" "vc-cascade-multi-select-field" "vc-cascade-select-field" "vc-alert" "vc-hovercard" "vc-button" "vc-tooltip" "vc-radio-field" "vc-repeat" "vc-select-field" "vc-group-layout" "vc-timeline" "vc-search-bar" "vc-number-info" "vc-steps" "vc-city-select-field" "vc-footer-toolbar" "vc-country-select-field" "vc-nav-bar" "vc-icon" "vc-html" "vc-condition-container" "vc-carousel" "vc-text" "vc-tree-select-field" "vc-tree" "vc-image" "vc-menu" "vc-money-input-field" "vc-multi-select-field" "vc-employee-field" "vc-tabs-layout" "vc-pickable-field" "vc-editor-field" "vc-filter" "vc-table-pc" "vs-metadata-select" "vs-jsx" "vs-link" "vs-event" "vs-style" "vs-image" "vs-validation" "vs-text" "vs-action" "vs-button" "vs-data-source" "vs-icon" "vs-editor" "vs-skin" "vs-select" "vs-options" "vs-number" "vs-list" "vs-json" "vs-i18n" "vs-date" "vs-color" "vs-code" "vs-choice" "vs-bool" "vu-field-property")
len=${#componentsList[*]}
declare -a notAuth=()

echo "----- 批量授权开始 -----\n";

# 循环执行
for ((i = 0; i < "$len"; i++))
do
  item=${componentsList[${i}]}
  echo "Adding @ali/${item} for fuxun.fx"
  tnpm owner add fuxun.fx @ali/${item} >> temp.txt 2>&1
  if [ $? -eq 0 ];then
    echo "\033[34m@ali/${item} successful \033[0m \n";
  else
    echo "\033[31m@ali/${item} failed \033[0m \n";
    notAuth[${i}]="${item}"
  fi
done

echo "----- 批量授权结束 -----\n"

echo "授权失败的列表:\033[42;37m${notAuth[@]}\033[0m \n"

Shell Commands - Tools and Bins

Awk & Sed

Node Apps

bower

A package management for the web.

  • bower ini start the project

grunt

Automatic JS TaskRunner

npm

Node Package Management

  • npm install NAME -g

express

The Express.js Framework Generator

browserify

Let the browser support the require/export style.

ionic

Mobile UI Framework

Shell Apps

brew

A package management for Mac Shell.

mysql

  • mysql -u USERNAME -p

mongo

Mongo Database Mangement

  • Mongo Series: Detail see the MongoDB/bin Directory

node

Node Console

jumbo

Baidu Package Management on Linux

  • jumbo search NAME
  • jumbo install NAME

Other Apps

jekyll

A Static-Blog Generator.

  • jekyll build
  • jekyll serve

cordova

PhoneGap Series

  • inoic

Shell Commands - File and Directory

Application Operation

  • type [BINNAME] Show the information of an executable file.
  • which [BINNAME] whereis shows the full path of (shell) commands.

Path Handler

  • pwd Show the current Absolute Path
  • cd Go to a path
  • tree
    • tree path -P PATTERN
      • tree /xxx/xx -P "*.sh"
    • tree path -I PATTERN
    • tree path -h print the size of directories and files.
  • mkdir
  • rmdir
  • ls -aul
    • ls -d Only show the directories, equals to ls -F | grep "/$"
  • chflags hidden FILE/DIRECTORY hidden file/dir on mac

File Operation

  • rm -fr -i i stands for interactive alert
  • mv change the document name or move file
  • cp -i i stands for interactive alert
  • ln —synbolic PATH PATH Make a link to the current object and becomes a mirror of it. (It’s a symbolic link)
    • example: ln -s base_config_dev.py config/base_config_default.py
  • tr [OPTIONS] set1 set2
    • echo "HELLO WORLD! | tr 'A-Z' 'a-z'
    • cat file.txt | tr -d -c '0-9'

File Preview

  • less
    • /: to find with Regular Expression
    • ?: to find with Regular Expression in a inverted way
    • <>: from the start or the end
    • space: next page
    • b: previous page
    • q: quit
    • g: top
    • G: bottom
  • head
  • tail
    • tail -f LOGFILE Dynamically watch the change of a file usually working with dynamically changed logs to detect errors.
  • cat
    • cat file1 file2 file3
    • echo 'Text through stdin' | cat - file.txt
# 从第3000行开始,显示1000行。即显示3000~3999行
cat filename | tail -n +3000 | head -n 1000
tail -n -1000:显示最后1000行
tail -n +1000:从1000行开始显示,显示1000行以后的
head -n 1000:显示前面1000行
  • sort Sort a file’s content and print it.
  • uniq report or filter out repeated lines in a file.
  • rsync Copy file with security

File Editor

  • vim It is a whole big world.
  • nano Easier one to edit a file.

File Property

  • chmod
  • chown
sudo chown -R $(whoami) /usr/local
  • chgrp
  • stat +FILE Check the status of a file
  • file FILENAME See the information of a file
  • du Show the disk usage
    • Linux: du --max-depth=1 -h Check the size of files and directories under current directory
    • Mac: du -d 1 -h

File Search and Match

  • qPATH [FILENAME]
    • find base_path
    • find . -print
    • find .. -print
    • find /xxx/xx -name "*.txt" -o -name "*.rtf" -print
    • find /xxx/ -path "*/xxxx/*" -print
    • find . -regex ".*\(\.py\|\.sh)$" -print
    • find . ! -name "*.txt" -print
    • find . -maxdepth 2 -name "f*" -print
    • Other Arguments: -type -atime -mtime -ctime -size +|- 2k -delete -perm
# recurisvely remove file with type
find . -type f -name '*.o' -delete
# check how many ts files under /src
find ./src -type f -name '**.ts' | wc -l
  • grep [options] PATTERN [FILE…] Match the searched items.
    • grep PATTERN FILENAME
      • grep -r 'string' * 递归查找当前目录下含有 string 的文本文件
    • echo -e "this is a word" | grep word
    • grep "MATCH_TEXT" file1 file2 file3
    • grep word filename --color=auto
    • grep -E "REGEXP" filename/path/symbols
# Search file contains IP Information
grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' ./*

File Operation

Compress and Extract

  • tar
    • tar -cvf ARCHIVE [FILE...]
    • tar -xf ARCHIVE extract the archive
    • tvf extract the archive.
      • 压缩 tar.gz 包:tar zcvf xxx.tar.gz xxx
      • 解压 tar.gz 包:tar zxvf xxx.tar.gz
  • bzip2
    • -v FILEANAM zip file.
  • bzcat | bunzip2 uncompressed a bzip2 zip file.
  • gzip
  • Don’t fuzzy the gzip and zip on Windows

Diff

  • cmp
  • diff -u | colordiff

Shell Commands - OS

OS Management Commands

System Operations Commands

  • shutdown -r -h -k -c time "info"

Screen Commands

  • clear
  • echo
  • sleep TIME sleep the shell

System Information Command

  • lsb_release -a Check the Linux Core/Version
  • df -ah Check the storage information
  • free -g Display amount of free and used memory in the system
    • -mt
  • man [bash] Looking for help
  • info COMMAND Looking for help
  • hostname Show the hostname
  • date Show the date of OS
  • uptime The system up time
  • locale The info of the locale
  • top Dynamic show the CPU status

Process Related Command

  • ps aux Check all of the current process
  • kill PID Kill the process with a PID.
  • pkill NAME Kill the process by name
  • bg PID Bring the process to the background.
  • fg PID Bring the process to the foreground.

User Management

  • id
  • who | w | finger [USERNAME] Show who is logged on.
    • who am i
  • sudo Execute a command as another user.
  • su (In some Sys.) Play the role of root user.
  • passwd [USER] Set root / user’s password.
# change password for root
sudo passwd root
  • write send message to another user
  • mesg close the message display

Group Management

Network Tools

  • ipconfig Show the Network and its device information.
    • ifconfig wlan0 192.168.0.80 netmask 255.255.252.0
    • ifconfig iface_name
  • dhclient eth0
  • netstat Query for the current network status.
  • cat /etc/resolv.conf
  • ping google.com
  • host
  • nslookup
  • route Show the table of router
  • OpenSSH
    • ssh username@DOMAIN => See the info or man
      • ssh -v username@DOMAIN will see the concrete debug information.
    • scp USERNAME@REMOTEHOST:path/path/file LOCALPATH Security Copy file from remote.
    • sftp => See the info or man
    • ~/.ssh/config and /etc/ssh/ssh_config
    • ssh-agent

Device Management

  • mount
  • unmount
  • lpr Print file with printer.
  • lpq Display the job queue of a printer.
  • lprm Cancel print jobs