03 Shell
03 Shell
2
UNIX Kernel and Shell (2)
由shell請kernel控制hardware
EXECUTE
Interpret
3
The UNIX Shells
Shell Originator System Name Prompt
Bourne Shell S. R. Bourne /bin/sh $
(In FreeBSD base) FreeBSD ⼀裝就有
4
Windows Shell
● cmd.exe
○ First released in 1987
○ For Windows NT/Windows CE
○ Still used in modern Windows
● PowerShell
○ First released in 2006
○ To provide the same functionality as UNIX shells
○ Also has Linux/MacOS releases
5
Shell Startup Files 每次使⽤這個Shell都會執⾏
例如:開機時或切換shell時
6
Shell Startup Files (2)
~/.tcshrc login shell
tcsh
(csh startup files) backward compatibility for csh
/etc/profile login shell
➔ ~/.bash_profile
➔ ~/.bash_login
bash ➔ ~/.profile
~/.bashrc login shell
BASH_ENV
Bash Startup Files : https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html
7
Shell Startup Files (3)
● A sample tcshrc for you to change your prompt
● Simplest install steps
○ Take a look at the content before running it
$ fetch https://nasa.cs.nctu.edu.tw/sa/sample/.tcshrc.color -o
~/.tcshrc
$ source ~/.tcshrc
8
Shell Environment Variables (1)
透過設定環境變數,可以改變 Shell 的⾏為
11
Variables and Strings Quotes (2)
Shell sh Csh
$ varname=`/bin/date` $ set varname=`/bin/date`
$ echo $varname $ echo $varname
Commands $ echo 'Now is $varname' $ echo 'Now is $varname'
$ echo "Now is $varname" $ echo "Now is $varname"
12
Global Variables
● Use "env" command to display global variables
● Assignment
13
Shell Special Characters
● Reduce typing as much as possible
Characters Description
* Match any string of characters
? Match any single alphanumeric character
sh
[…] Match any single character within []
[!...] Match any single character not in []
~ Home directory
14
Shell Special Characters (2)
● Example: There are some files in current directory
○ test1, test2, test3, test4, test-5, testmess
Command Result
$ ls test* test1 test2 test3 test4 test-5 testmess
$ ls test? test1 test2 test3 test4
sh
$ ls test[123] test1 test2 test3
$ ls test[!345]* test1 test2 test-5 testmess
$ ls ~ List files under your home
15
Shell Special Characters (3)
Char. Purpose Example
# Start a shell comment # this is a comment
; Command separator $ ls test*; ls test?
Executes the first command, and then executes the
&& $ cd foo/bar && make install
second if first command success (exit code=0)
Executes the first command, and then executes the
|| $ cp x y || touch y
second if first command fail (exit code≠0)
16
Shell Special Characters (4)
Char. Purpose Example
$ touch test\*; ls test\*
(1)Escape character test*
\
(2)Command continuation indicator $ ls \
> test*
$ make buildworld &
& Background execution
$ sleep 5 &
17
Common Built-in Commands
SH CSH Description
Set/Unset shell options and positional
set/unset
parameters
(empty)/unset set/unset Set/Unset a local variable
export setenv/unsetenv Set/Unset a global variable
Display shell variables
set
(sh: local + global, csh: local)
env Display global (environment) variables
(N/A) login, logout Login / Logout
18
Common Built-in Commands (2)
SH CSH Description
(N/A) dirs print directory stack
(N/A) popd, pushd Pop/push directory stack
19
Built-in Shell Commands (3)
SH CSH Description
20
Built-in Shell Commands (4)
SH CSH Description
References:
● https://it.cs.nycu.edu.tw/unix-basic-commands
● http://www.unix.org.ua/orelly/unix/unixnut/ch04_06.htm
● http://publib.boulder.ibm.com/infocenter/pseries/index.jsp?topic=/com.ibm.aix.doc/aixuser/usrosde
v/list_c_builtin_cmds.htm
● https://www.freebsd.org/cgi/man.cgi?query=tcsh
● https://www.freebsd.org/cgi/man.cgi?query=sh
21
Input/Output Redirection
● There are 3 default file descriptors
23
File and Directory Related Commands
Command Purpose
ls List a directory's content
pwd Print working directory
cd Change to other directory
mkdir Make(create) a new directory
rmdir Remove existing empty directory
cat Concatenate file
cp Copy file
24
File and Directory Related Commands (2)
Command Purpose
ln Link files
mv Move file
rm Remove file
stat Display file status
25
Select and File Processing Related
Commands
Command Purpose
head Display first lines of a file
tail Select trailing lines
grep Select lines
diff Compare and select difference in two files
wc Count characters, words or lines of a file
uniq Select uniq lines
cut Select columns
26
Select and File Processing Related
Commands (2)
Command Purpose
sort Sort and merge multiple files together
sed Edit streams of data
awk Pattern scanning and processing language
27
Select and File Processing Related
Commands (3) - Example Usage
● Look first few lines or last few lines
○ $ head /var/log/message
○ $ tail /var/log/message
■ -n : specific how many lines
● Find the occurrence of certain pattern in file
○ $ grep -l tsaimh *
■ Print the filename that has “tsaimh" as content
○ $ grep -n tsaimh /etc/passwd
■ Print the line number when using grep
28
Select and File Processing Related
Commands (4) - Example Usage
● List tsaimh’s id, uid, home, shell in /etc/passwd
○ $ grep tsaimh /etc/passwd | cut -f1,3,6,7 -d:
■ -f1,3,6,7 : fetch 1st ,3rd ,6th ,7th column
■ -d : separation symbol
tsaimh:*:1001:20:Meng-Hsun Tsai:/home/tsaimh:/bin/tcsh
29
Select and File Processing Related
Commands (5) - Example Usage
● Cut out file permission and file name from ls output
○ $ ls -l | grep -v ^total | cut -c 1-11,47-
■ -c1-11:1st~11th characters (start from 1, instead of 0)
■ -c47-:characters after 47th character (include 47th )
$ ls -l
total 2312
-rw-r--r-- 1 tsaimh ta 875394 Aug 14 13:37 00_Syllabus.pdf
-rw-r--r-- 1 tsaimh ta 841270 Aug 12 15:59 01_Install_FreeBSD.pdf
-rw-r--r-- 1 tsaimh ta 457582 Aug 12 15:59 02_Installing_Applications.pdf
$ ls -l | grep -v ^total | cut -c 1-11,47-
-rw-r--r-- 00_Syllabus.pdf
-rw-r--r-- 01_Install_FreeBSD.pdf
-rw-r--r-- 02_Installing_Applications.pdf 30
Select and File Processing Related
Commands (6) - Example Usage
● Use awk to generate the same behavior of cut
○ $ ls -l | grep -v ^total | awk '{print $1 " " $9}'
■ Result is same as P.30
$ ls -l
total 2312
-rw-r--r-- 1 tsaimh ta 875394 Aug 14 13:37 00_Syllabus.pdf
-rw-r--r-- 1 tsaimh ta 841270 Aug 12 15:59 01_Install_FreeBSD.pdf
-rw-r--r-- 1 tsaimh ta 457582 Aug 12 15:59 02_Installing_Applications.pdf
$ ls -l | grep -v ^total | awk '{print $1 " " $9}'
-rw-r--r-- 00_Syllabus.pdf
-rw-r--r-- 01_Install_FreeBSD.pdf
-rw-r--r-- 02_Installing_Applications.pdf
31
Select and File Processing Related
Commands (7) - Example Usage
● Use awk to generate the same behavior of cut
○ $ awk -F: '{print $1 " " $6}' /etc/passwd
■ -F :separation symbol
tsaimh:*:1001:20:Meng-Hsun Tsai:/home/tsaimh:/bin/tcsh
32
Select and File Processing Related
Commands (8) - Example Usage
● Options of "sort" command
○ -r : reverse
○ -u : unique keys
○ -n : numeric keys sorting
■ Default: string sorting, 14 > 123
○ -k : specific columns to sort with
○ -t : field separator
33
Select and File Processing Related
Commands (9) - Example Usage
● List directory contents and sort by file size decreasingly
○ $ ls -al | sort -n -k 5,5 -r
■ -k : specific columns to sort with
■ -r : reverse
% ls -l | sort -n -k 5,5 -r
-rw-r--r-- 1 tsaimh ta 875394 Aug 14 13:37 00_Syllabus.pdf
-rw-r--r-- 1 tsaimh ta 841270 Aug 12 15:59 01_Install_FreeBSD.pdf
-rw-r--r-- 1 tsaimh ta 457582 Aug 12 15:59 02_Installing_Applications.pdf
34
Select and File Processing Related
Commands (10) - Example Usage
● Sort contents of /etc/passwd by username and remove annotations
○ $ sort -t: -k 1,1 /etc/passwd | grep -v ^#
■ -t : field separator
■ -k : specific columns to sort with
games:*:7:13:Games pseudo-user:/usr/games:/usr/sbin/nologin
git_daemon:*:964:964:git daemon:/nonexistent:/usr/sbin/nologin
hast:*:845:845:HAST unprivileged user:/var/empty:/usr/sbin/nologin
kmem:*:5:65533:KMem Sandbox:/:/usr/sbin/nologin
tsaimh:*:1001:20:Meng-Hsun Tsai:/home/tsaimh:/bin/tcsh
35
Select and File Processing Related
Commands (11) - Example Usage
● List records in /etc/hosts sorted by IPv4 address
$ sort -t. -n -k 1,1 -k 2,2 -k 3,3 -k 4,4 '/etc/hosts' | grep -v ^#
■ -n : numeric keys sorting
● Before sorting
# In the presence of the domain name service or NIS, this file may
# not be consulted at all; see /etc/nsswitch.conf for the
# resolution order.
#
::1 localhost localhost.my.domain
127.0.0.1 localhost localhost.my.domain
140.113.17.26 nctucs.tw
64.233.187.95 www.googleapis.com googleapis.l.google.com
36
Select and File Processing Related
Commands (12) - Example Usage
● List records in /etc/hosts sorted by IPv4 address
$ sort -t. -n -k 1,1 -k 2,2 -k 3,3 -k 4,4 '/etc/hosts' | grep -v ^#
■ -n : numeric keys sorting
● After sorting
::1 localhost localhost.my.domain
64.233.187.95 www.googleapis.com googleapis.l.google.com
127.0.0.1 localhost localhost.my.domain
140.113.17.26 nctucs.tw
37
Select and File Processing Related
Commands (13) - Example Usage
● Translate characters
○ $ echo "Hello World" | tr "a-z" "A-Z"
■ Change all alphabet to uppercase
$ echo "Hello World" | tr "a-z" "A-Z"
HELLO WORLD
40
xargs Command (2)
% ls
2.sh 3.csh 4.csh 4.sh bsd1.ping
testin
% ls | xargs echo
2.sh 3.csh 4.csh 4.sh bsd1.ping testin
% ls | xargs -n1 echo
2.sh
3.csh
4.csh
4.sh
bsd1.ping
testin
41
xargs Command (3)
% ls | xargs -I % -n1 echo % here %
2.sh here 2.sh
3.csh here 3.csh
4.csh here 4.csh
4.sh here 4.sh
bsd1.ping here bsd1.ping
testin here testin
42
xargs Command (4)
% ls | xargs -J % -n1 echo % here %
2.sh here %
3.csh here %
4.csh here %
4.sh here %
bsd1.ping here %
testin here %
43
xargs Command (5)
● Example : ping all hosts in file
$ cat host
www.google.com
bsd1.cs.nctu.edu.tw
linux3.cs.nctu.edu.tw
cs.nctu.edu.tw
45
ShellCheck
● Finds bugs in your shell scripts
● https://www.shellcheck.net/
● devel/hs-ShellCheck
● pkg install hs-ShellCheck
46
Appendix
Command History in (t)csh
國立陽明交通⼤學資⼯系資訊中⼼
Information Technology Center, Department of Computer Science, NYCU
Command History in (t)csh
Commands Description
!n exec previous command line n (see history)
!-n exec current command line minus n
!! exec last command (the same as !-1)
!str exec previous command line beginning with str
!?str exec previous command line containing str
% history
10 8:31 cp ypwhich.1 ypwhich.1.old
11 8:31 vi ypwhich.1
12 8:32 diff ypwhich.1.old ypwhich.1
13 8:32 history
% !?old 48
Command History in (t)csh (2)
Commands Description
!!:n use the nth word of previous comm
!!:m-n select words m ~ n of previous command
!!:* use all arguments of previous command
!!:s/str1/str2/ substitute str1 with str2 in previous command
% history
15 8:35 cd /etc
16 8:35 ls HOSTS FSTAB
17 8:35 history
% cat !-2:*:s/HOSTS/hosts/:s/FSTAB/fstab → cat hosts fstab