Created
March 29, 2011 18:10
-
-
Save paulchubatyy/892891 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
################################################## Basic | |
# this is comment | |
########### variable (quoting a variable preserves whitequotes) | |
# USERNAME='lzyy' | |
# | |
# echo ${USERNAME} | |
# echo ${USERNAME=`whoami`} | |
########### separator | |
# echo 'hello'; echo 'world'; | |
########### clear & append to file | |
# > /tmp/nothing | |
# >> /tmp/append | |
# < /input/file | |
########### {} | |
# cat {file1,file2,file3} > file | |
# cp file11.{txt,backup} | |
########### [] | |
# if [ -z "$1" ];then | |
# fi | |
# if [ "$exp1" -a "$exp2" ];then | |
# ... | |
# fi | |
# | |
# if [ "$exp1" -o "$exp2" ];then | |
# ... | |
# fi | |
# arr[1] = 'ifeng' | |
# echo ${arr[1]} | |
########### xargs and {} | |
# ls . | xargs -I file cp -r file ../ | |
################################################## Variable | |
########### undefined variable | |
# if [ -z "$var" ];then | |
# echo "\$var is NULL" | |
# fi | |
########### variable assignment | |
# a=`echo hello` | |
########### magic ${...} | |
# a=5566 | |
# b=${a/55/ss} | |
# echo $b | |
# ${#string} # string length | |
# ${string:position:length} | |
# ${string:-default} # '-' means default, if you want right index, put brace around | |
# ${string:(-4)} | |
# ${string#substring} # deletes shortest match of $substring from front of $string | |
# ${string##substring} # deletes longest match of $substring from front of $string | |
# ${string%substring} # backend shortest | |
# ${string%%substring} # backend longest | |
# ${string/substring/replacement} # first | |
# ${string//substring//replacement} # all | |
########### Arguments passed to the script | |
# $0, $1, $2 # $0 is script file name | |
# $# # param num | |
# if [ -z "$1" ];then | |
# echo "\$1 is empty" | |
# fi | |
################################################## Condition && loop | |
########### if elif else | |
# if [ ... ]; then | |
# ... | |
# elif | |
# ... | |
# else | |
# ... | |
# fi | |
########### file test | |
# -e # file exists | |
# -f # is a regular file | |
########### int test | |
# -eq # equal to | |
# -ne # not equal | |
# -gt # greater than | |
# -lt # less than | |
########### string test | |
# = # equal to | |
# != # not equal to | |
# < # less than (ascii order) | |
# > # less than (ascii order) | |
# -z # string is null | |
# -n # string is not null | |
########### while & for loop | |
# while [ ... ]; do # while read line; do | |
# ... | |
# done | |
# | |
# for item in $var; do # $var is separator by ' ' | |
# ... | |
# done | |
################################################## Function | |
# func () | |
# { | |
# local var # if not declare as 'local', it is defined as a global var (like js) | |
# ...$1 $2 | |
# } | |
# | |
# func param1 param2 # function call is like bash call | |
# var="a | |
# b | |
# c" | |
# | |
# echo ${#var} | |
## if you want return val, echo to std | |
# func() | |
# { | |
# echo 'hello world'; | |
# } | |
# | |
# return_val=$(func) | |
# | |
# echo ${return_val} | |
################################################## Date | |
# date +%j # + start format | |
# date "+%Y-%m-%d %H:%M:%S" | |
################################################## Array | |
# area[11]=15 | |
# area[13]=36 | |
# area[20]='hello' | |
# | |
# area=(hello 1 3) | |
# echo ${area[11]} | |
# echo ${area[@]} | |
# echo ${area[@]:position:length} | |
################################################## Utils | |
## internal variables | |
# export # makes a variable available to all child processes | |
# wait # Suspend script execution until all jobs running in background have terminated | |
## useful cmd | |
# find | |
# date | |
# uniq | |
# head | |
# tail | |
# grep | |
# sort | |
# sed (sed -i '' -e's/search/replace/g' file) | |
# awk (awk -F: '{print $2}') | |
# wc (wc -l / -c) | |
# tar/gzip/zip | |
# which/whereis | |
# diff/patch (patch -p0 xxx.patch) | |
# basename/dirname | |
# mktemp | |
# wget | |
# rsync (rsync -avz source dest) | |
# top | |
# pgrep/pkill | |
# ps (ps ax) | |
# watch | |
################################################## Advanced | |
########### heredoc | |
# >test.txt <<EOT | |
# what | |
# a | |
# wonderful | |
# world | |
# EOT | |
########### subshell | |
# outer='hello outer' | |
# | |
# ( | |
# echo ${outer} | |
# ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment