Skip to content

Latest commit

 

History

History
125 lines (106 loc) · 4.1 KB

ShellNotes.org

File metadata and controls

125 lines (106 loc) · 4.1 KB

Shell Programming Notes

1 Basics

1.1 Pre knowledge

# chmod +rx scriptname
echo "Here" # A whitespace before '#' in comment
echo -n $var # echo variable without newline character

hello="A B  C   D"
echo $hello   # A B C D       (variable reference)
echo "$hello" # A B  C   D    (variable itself)

if [ -n "${10}" ]  # Parameters > $9 must be enclosed in {brackets}.

arch=$(uname -m) # get current OS's arch

if [ ! -f "$filename" ]   # Quoting $filename allows for possible spaces.

find ~/ -name 'core*' -exec rm {} \;
# Removes all core dump files from user's home directory.
  • Ctrl-H Erases characters back
  • Bash sets the “integer value” of a string to 0.
  • $# : number of parameters
  • $*/$@ : all the positional parameters. P778
  • P53 : get keyboard input

1.2 Redirect

1>filename
   # Redirect stdout to file "filename."
1>>filename
   # Redirect and append stdout to file "filename."
2>filename
   # Redirect stderr to file "filename."
2>>filename
   # Redirect and append stderr to file "filename."
&>filename
   # Redirect both stdout and stderr to file "filename."
   # This operator is now functional, as of Bash 4, final release.
M>N
  # "M" is a file descriptor, which defaults to 1, if not explicitly set.
  # "N" is a filename.
  # File descriptor "M" is redirect to file "N."
M>&N
  # "M" is a file descriptor, which defaults to 1, if not set.
  # "N" is another file descriptor.
  # 2>&1 ==> Redirects stderr to stdout. 
>&j
# Redirects, by default, file descriptor 1 (stdout) to j. # All stdout gets sent to file pointed to by j.

2 Useful Tips

  • space do matters
  • Sed almost always runs faster than awk
  • tree ignore by pattern link $tree -I ‘node_module*’ to ignore node_modules/
  • mess stuff
    cat {file1,file2,file3} > combined_file
    # Concatenates the files file1, file2, and file3 into combined_file.
     
    cp file22.{txt,backup}
    # Copies "file22.txt" to "file22.backup"
     
    (cd /source/directory && tar cf - . ) | (cd /dest/directory && tar xpvf -)
    # Move entire file tree from one directory to another 
    # abs-guide P28
     
    echo `date` | pbcopy
    # get date string into clipboard 
     
    read -p "Want to start? (y/n) " res
    if [ "$res" = "y"]; then
        # do blah blah
    else
        # do blah blah
    fi
        

3 Archieve

3.1 Create

$tar -cvf filename.tar ./path/file (dir) <–不压缩 $tar -zcvf filename.tar.gz ./path/file (dir) <–以 gzip 压缩 $tar -jcvf filename.tar.bz2 ./path/file (dir) <–以 bzip2压缩

3.2 Unzip

$tar -xvf filename.tar $tar -zxvf filename.tar.gz <–注意后缀要对应写正确 $tar -jxvf filename.tar.bz2

3.3 tee

  • pipe standard output to terminal and new file at same time ls | tee new_file
  • executes the content of the file passed as argument

3.5 圖片白色轉透明

  • mogrify -transparent white image*.png

3.6 Execute founded files

  • find . -name “*.pyc” –exec rm -rf {} \; # delete files recursively
  • find . -name “.DS_Store” -print0 | xargs -0 rm -rf # delete directories recursively # print0 ensure file name contains space interpreted corretly
  • find . -depth 1 -name ”.py” –exec cp ‘{}’ ./destination \; *in OSX, is -exec [only one dash]

3.7 execute find results

3.8 colorful less

  • less file, then press v