My Bash Notes

1 Common Syntax

1.1 case

case "$var" in
    pattern_1)
        commands
        ;;
    pattern_2)
        commands
        ;;
esac

1.2 if

if [[ ]]; then
    commands
elif [[ ]]; then
    commands
else
    commands
fi

1.2.1 Logical Operators

-eq # equal
-ne # not equal
-lt # less than
-le # less than or equal
-gt # greater than
-ge # greater than or equal
[[ $num1 -eq $num2 ]] && echo "true"

1.3 Conditionals (Square brackets)

[ -f file.txt ] && echo "File exist" 
[ -s FB_title.txt ] && echo "File size > 0"
[ -d dir/ ] && echo "Dir exist"
[ -z "$var" ]      # Whether $var is empty

1.4 Loops

1.4.1 for

1.4.1.1 With Sequence of Numbers

Tradional Type

for i in {1..3}; do
    commands
done

C lang Type

for (( i=0; i<=5; ++i )); do
    commands
done

1.4.1.2 Other Sequences

for i in A B C D; do
    commands
done
for i in {A..D}{1..3}; do
    commands
done

2 Common Techniques

2.1 Parameter Expansions

2.1.1 Match the last expresion

> ~/Desktop/foo.txt
file_path=~/Desktop/foo.txt
echo ${file_path##*/}
> foo.txt

2.2 Copy Previous Command to file

cat ~/bash_pract/rmd-head.txt >> ~/bash_pract/doc.Rmd
echo "!!" >> ~/bash_pract/cmd_hist

Then the command appends literal cat ~/bash_pract/rmd-head.txt >> ~/bash_pract/doc.Rmd to the end of cmd_hist. Note that this works only on the command line, not shell scripts.

2.3 Expansion with text

Download all web pages starting from www.millionbook.net/wx/j/jingyong/sdxl/001.htm to www.millionbook.net/wx/j/jingyong/sdxl/041.htm:

url=http://www.millionbook.net/wx/j/jingyong/sdxl/
wget "$url"{001..041}".htm"

3 RegEx

MetaCharacters

  • .: Any character.

  • +: 1 or more, e.g. s+aa matches 1 or more s followed by aa.

  • *: 0 or more, e.g. s*aa matches 0 or more s followed by aa.

  • {#}: Exact #, e.g. s{2} matches strings with exactly 2 s(equivalent to ss).
    • {#_1, #_2}: **Between #_1 & #_2**.
  • (<some.char>): Use ( & ) to group
    • (na){2}: matches banana.

4 String Manipulation

4.1 cut

list.txt:

10/04 2018,xaringan
09/28 2018,pttR-intro

Read one line of list.txt in each loop, cut the line into 2 fields, seperated by ,.

while read p; do
  date=$(echo "$p" | cut -d ',' -f 1)
  file=$(echo "$p" | cut -d ',' -f 2)
done < list.txt

4.2 printf

More consistent then echo, can use special characters like \\n.

printf "$date &nbsp;&nbsp; [$file](${file}/index.html)\n\n"

4.3 cat

cat the text between the two _EOF_.

cat << _EOF_
---
title: Slides
---

# List of Slides
<br><br>
_EOF_

cat the text between the two _EOF_, then redirect the text to index.md.

cat >> index.md  << _EOF_
---
title: Slides
---

# List of Slides
<br><br>
_EOF_

4.4 grep

Search files in working dir that contain certain string:

grep -r ".img" *

Search file names in working dir that contain certain string:

ls | grep ".txt"

4.5 Compare Files

ls dir1 > file1.txt
ls dir2 > file2.txt
diff file1.txt file2.txt

6 Reading Files

6.1 Loop Over Every Line of a File

while read p; do
  date=$(echo "$p" | cut -d ',' -f 1)
  file=$(echo "$p" | cut -d ',' -f 2)
done < list.txt

6.2 Loop Over Several Files

while IFS=$'\t' read -r title tags link
do
  printf "${title}/n"
  printf "${tags}/n"
  printf "${link}/n/n"
done < <(paste FB_title.txt FB_tags.txt FB_link.txt)

7 Function Flows

[ -f file.txt ] && exit 0
sleep 10 # seconds

8 Network

8.1 Curl

curl --silent --show-error --fail https://raw.githubusercontent.com/Rbloggers/web/posts/FB_title.txt > FB_title.txt
  • --silent: Don’t print process to std. output
  • --show-error: Print error message to std. output
  • --fail: Return empty file if 404

8.2 Setup SSH

Server Setup

Install SSH Server (so be connected by a remote client):

sudo apt-get install openssh-server

Configure:

sudo gedit /etc/ssh/sshd_config

Port 22

PasswordAuthentication yes

PermitRootLogin yes

Findout IP address locally: hostname -I

Client Setup

Access from remote via SSH:

Syntax: ssh <server-user-account>@<ip>

e.g.

ssh liao961120@192.168.2.110

9 Remote Control Via SSH

9.1 Hardware Configuration

Enable WakeOnLane(WOL) at BIOS:

  1. Power Management Setup > WOL: enabled1
  2. Power Management Setup > deep power off mode: disabled2

9.2 wakeonlan

wakeonlan -i

wakeonlan -i 192.168.2.255 44:87:fc:a1:0b:cc
wakeonlan -i $rm_ip_b $rm_MAC

9.3 SSH login

ssh liao961120@

ssh liao961120@192.168.2.110
ssh $ssh_pc

9.4 SSH logout & suspend computer

Use sudo shutdown 0 to leave. Remember to enable Power of Mode in BIOS, so you can WOL to open the computer again.

9.5 Variable Definition (in ~/.bashrc)

rm_MAC="44:87:fc:a1:0b:cc"
rm_ip="192.168.2.110"
rm_ip_b=`bash ~/ip2ipb.sh $rm_ip`
ssh_pc="liao961120@$rm_ip"

10 Other Resources


  1. There may be different names for WOL, such as ‘Wake Up on Lan’, ‘Automatic Power Up’, ‘Resume by LAN’, ‘Power Up On PCI Device’, ‘PME Event Wake Up’, etc.

  2. This keeps the power for NIS to turn on the computer when receiving a signal from web.

Yongfu Liao

May 02, 2020