Shell Commands - Bash
Bash Window
Ctrl + amove the cursor to the first character of the current lineCtrl + emove the cursor to the last character of the current lineCtrl + udelete all charactersCtrl + kdelete all characters following by the cursor
Programming with Bash
Operation Control Symbols
x ; y ; zd & e & fg | h | i &pipe the stream to commandsa && b >> /log- write content to pipe into a file
# redirectcat file > copy_file# append itcat file >> copy_fileecho 'Hello, World' > file
(a ; b) & (c ; d) &Group tasks\continue input the command<<>>write in command|&''""
Task Controls
jobslist the tasks that are running.fgID move the job to the front with specific ID.CTRL + Z/CStopped Jobbg
Stack of the Operation Directives
dirspushdpwdpopd
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.
printenvshow the env variablesname=('a' 'b' 'c')Array Variableecho ${A[n]}n is a Numberecho ${A[@]}andecho ${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"thenecho “do something!”elifthenecho "do cool thing!"elseecho “do something else!”fi
- For in
for loop-index in argument-listdo# commands heredone
- For
for loop-indexdo# commands heredone
- While
while test-commanddo# commands heredone
- Until
util test-commanddo# commands heredone
- break | continue
- case
case test-string inpattern1)commands-1;;pattern2)commands-2;;
…esac
Inner commands
readUser input handlingechoOutputexportSet Global Env. VariabledeclareSet Variable and its propertytypeShow the related information of a commandexecExecute a command or redirect identifier of a filetrapCapture signaltrap [commands] [singal]
killKill ProcessevalScan / Give value and execute commandsgetoptsParsing optionssedEditorAWKPattern 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 inistart 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
pwdShow the current Absolute PathcdGo to a pathtreetree path -P PATTERNtree /xxx/xx -P "*.sh"
tree path -I PATTERNtree path -hprint the size of directories and files.
mkdirrmdirls -aulls -dOnly show the directories, equals tols -F | grep "/$"
chflags hidden FILE/DIRECTORYhidden file/dir on mac
File Operation
rm -fr -ii stands for interactive alertmvchange the document name or move filecp -ii stands for interactive alertln—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
- example:
tr [OPTIONS] set1 set2echo "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 LOGFILEDynamically watch the change of a file usually working with dynamically changed logs to detect errors.
- cat
cat file1 file2 file3echo '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.
rsyncCopy file with security
File Editor
- vim It is a whole big world.
- nano Easier one to edit a file.
File Property
chmodchown
sudo chown -R $(whoami) /usr/local
chgrpstat +FILECheck the status of a filefile FILENAMESee the information of a fileduShow the disk usage- Linux:
du --max-depth=1 -hCheck the size of files and directories under current directory - Mac:
du -d 1 -h
- Linux:
File Search and Match
- qPATH [FILENAME]
find base_pathfind . -printfind .. -printfind /xxx/xx -name "*.txt" -o -name "*.rtf" -printfind /xxx/ -path "*/xxxx/*" -printfind . -regex ".*\(\.py\|\.sh)$" -printfind . ! -name "*.txt" -printfind . -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 FILENAMEgrep -r 'string' *递归查找当前目录下含有 string 的文本文件
echo -e "this is a word" | grep wordgrep "MATCH_TEXT" file1 file2 file3grep word filename --color=autogrep -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 -xfARCHIVE extract the archivetvfextract the archive.- 压缩 tar.gz 包:
tar zcvf xxx.tar.gz xxx - 解压 tar.gz 包:
tar zxvf xxx.tar.gz
- 压缩 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
clearechosleep TIMEsleep the shell
System Information Command
lsb_release -aCheck the Linux Core/Versiondf -ahCheck the storage informationfree -gDisplay amount of free and used memory in the system- -mt
man [bash]Looking for helpinfo COMMANDLooking for helphostnameShow the hostnamedateShow the date of OSuptimeThe system up timelocaleThe info of the localetopDynamic show the CPU status
Process Related Command
ps auxCheck all of the current processkill PIDKill the process with a PID.pkill NAMEKill the process by namebg PIDBring the process to the background.fg PIDBring the process to the foreground.
User Management
idwho | w | finger [USERNAME]Show who is logged on.who am i
sudoExecute 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
mesgclose 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.0ifconfig iface_name
dhclient eth0- netstat Query for the current network status.
cat /etc/resolv.confping google.comhostnslookuprouteShow the table of router- OpenSSH
ssh username@DOMAIN=> See the info or manssh -v username@DOMAINwill see the concrete debug information.
- scp USERNAME@REMOTEHOST:path/path/file LOCALPATH Security Copy file from remote.
sftp=> See the info or man~/.ssh/configand/etc/ssh/ssh_configssh-agent
Device Management
mountunmountlprPrint file with printer.lpqDisplay the job queue of a printer.lprmCancel print jobs
