shell
shell是核心程序kernel之外的指令解析器,是一個程序,同時是一種命令語言和程序設計語言。
shell是命令解析器,用戶輸入命令,它去解析。
shell類型 ash、bash、ksh、csh、tcsh
shell類型 ash、bash、ksh、csh、tcsh
cat /etc/shells 看系統下的shell
echo $SHELL 看當前用戶運行的shell
hzmct@U-64:/study/linuxtest/day03$ cat /etc/shells
# /etc/shells: valid login shells
/bin/sh
/bin/dash
/bin/bash
/bin/rbash
/bin/ksh93
/bin/rksh93
hzmct@U-64:/study/linuxtest/day03$ help
hzmct@U-64:/study/linuxtest/day03$ echo $SHELL
/bin/bash
hzmct@U-64:/study/linuxtest/day03$ /bin/dash
$
hzmct@U-64:/study/linuxtest/day03$ /bin/dash
$ exit
hzmct@U-64:/study/linuxtest/day03$
程序在shell中運行
ls命令執行過程分析
shell程序讀取用戶的輸入,解析用戶的輸入字符,然後搜索PATH這個全局變量保存的路徑,尋找ls命令,若找到,則fork()一個子進程,調用exec()加載ls命令
存取權限與安全
-d目錄
–一般文件
–l快捷方式
–c字符設備
–b塊設備
-s-socket設備文件
–p管道文件
chown
1、更改用戶名和用戶組名
chown 用戶名.用戶組名 文件名
chown .用戶組名 文件名
chown 用戶名.用戶組名 -R 文件夾名
chown .hzmct 4.txt//注意有個點
//改變用戶組名
chgrp root 4.txt
chgrp -R root 文件夾
chmod
chmod [who] operator [permission] filename
who(u用戶 , g用戶組, o其他, a所有用戶)
operator(+增加權限, -取消權限, = 賦予給定權限並取消其他權限)
permission(r可讀, w可寫, x可執行, s具有超級用戶, t執行文件在緩存)
chmod有兩種用法 (The format of a symbolic mode)
1、 數字方式
用戶 用戶組其他 增加= 讀 寫 執行 s(具有超級用戶)t(執行文件在緩存)
eg:
chmod改變文件或者目錄的權限位
man chmod
r w x 數字代表 4 2 1
chmod u=rwx,g-x,o+xs 1.txt
注意:S位存在的意義:寫一個程序,被執行是,臨時擁有超級用戶權限,執行完畢以後,有恢復普通身份,例如,寫一個數據庫腳本,這個數據庫腳本具有超級管理員身份運行,運行完畢以後,恢復普通身份,避免破壞系統。
查看帶有s位的應用程序,命令 ls -l /bin | grep ‘^…s’
umask
umask命令,用來指定,用戶創建文件、目錄,默認的權限位
umask的默認配置 $HOME/.profile (ubuntu16.04)
hzmct@U-64:/study/linuxtest/day03$ umask
0002
hzmct@U-64:/study/linuxtest/day03$ touch 2.txt
hzmct@U-64:/study/linuxtest/day03$ ll
421
-rw-rw-r-- 1 hzmct hzmct 0 8月 28 18:24 2.txt
文件默認權限664
drwxrwxr-x 2 hzmct hzmct 4096 8月 28 18:25 2/
目錄默認權限 775
規律:如果 文件權限位=7-umask-1 目錄權限位=7-umask
ls –lda 命令只查看目錄
ls -lh -h易於人閱讀
硬連接和軟連接
ln source path targh path
ln -s source path targh path
-rwxrwxrwx 2 hzmct hzmct 281 8月 25 16:36 1.sh*
-rwxrwxrwx 2 hzmct hzmct 281 8月 25 16:36 硬鏈接.sh*
lrwxrwxrwx 1 hzmct hzmct 4 8月 25 16:38 軟連接.sh -> 1.sh*
(開頭是l表示是快捷方式)
.bashrc
source ./bashrc
. ./bashrc . 表示執行
alias
hzmct@U-64:~$ help alias
alias: alias [-p] [name[=value] ... ]
Define or display aliases.
Without arguments, `alias' prints the list of aliases in the reusable
form `alias NAME=VALUE' on standard output.
Otherwise, an alias is defined for each NAME whose VALUE is given.
A trailing space in VALUE causes the next word to be checked for
alias substitution when the alias is expanded.
Options:
-p Print all defined aliases in a reusable format
Exit Status:
alias returns true unless a NAME is supplied for which no alias has been
defined.
hzmct@U-64:~$ alias -p
alias alert='notify-send
alias egrep='egrep
alias fgrep='fgrep
alias grep='grep
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls
hzmct@U-64:~$ alias chen="pwd"
hzmct@U-64:~$ chen
/home/hzmct
wc
hzmct@U-64:/study/linuxtest/day03$ wc 1.txt
3 15 152 1.txt
行數 字數 字節數 文件名
hzmct@U-64:/study/linuxtest/day03$ cat 1.txt
hzmct tty7 2018-08-26 10:18 (:0)
hzmct pts/1 2018-08-26 10:20 (192.168.66.1)
hzmct pts/9 2018-08-26 11:25 (192.168.66.1)
hzmct@U-64:/study/linuxtest/day03$ wc -c 1.txt
152 1.txt
hzmct@U-64:/study/linuxtest/day03$ wc -l 1.txt
3 1.txt
hzmct@U-64:/study/linuxtest/day03$ wc -w 1.txt
15 1.txt
od
-
. -->
. -->
. -->
. -->
. -->
. -->
du
查看目錄的大小
hzmct@U-64:/study/linuxtest/day03$ du -h
4.0K ./mulu
4.0K ./2
72K .
df查看磁盤的使用情況
hzmct@U-64:/study/linuxtest/day03$ df -h
Filesystem Size Used Avail Use
udev 472M 0 472M 0
tmpfs 99M 6.4M 92M 7
/dev/sda1 19G 8.3G 9.4G 47
tmpfs 492M 188K 492M 1
tmpfs 5.0M 4.0K 5.0M 1
tmpfs 492M 0 492M 0
tmpfs 99M 36K 99M 1
tmpfs 99M 0 99M 0
which
查看指定命令所在的路徑
which指令會在PATH變量指定的路徑中,搜索某個系統命令的位置,並且返回第一個搜索結果。
cd 這個常用的命令竟然找不到啊!爲什麼呢? cd 是bash 內建的命令!
tar
//壓縮
tar zcvf 壓縮包名字.tar.gz 原材料(要打包壓縮的文件或目錄)
tar zcvf day01.tar.gz day01 > /dev/null &
//解壓
tar zxvf myfile.tar.gz -C 解壓目錄
查看裏面的文件
tar tvf 1.tar.gz
sort
sort < mufile.txt
sort < myfile.txt >myfile+sort.txt
touch my\ file
touch "wang bao ming"
shell本地變量
本地變量在用戶現有的shell生命週期中使用
定義本地變量:MYVAR=”test” 注意=左右不要有空格
退出終端,重新登錄,問,剛纔登錄的本地變量還存在嗎?
仔細思考本地變量背後的含義! echo $MYV 每登錄一個終端,都會運行一個shell程序,這個本地變量,就保存在這個shell程序中;如果再新登錄,將啓動新的shell程序,與原來的shell不同。建立起概念。
定義本地變量
var="helloworld"
使用本地變量
echo $var 或者 echo ${var}
設置本地變量的只讀屬性
readonly var="kkk"
查看剛纔定義的本地變量
set | grep var
顯示本地所有的變量
set
注意:本地變量只保存在當前的shell程序中
例子:
hzmct@U-64:/study/linuxtest/day03$ var="hello world"
hzmct@U-64:/study/linuxtest/day03$ sh
$ echo $var
$ exit
hzmct@U-64:/study/linuxtest/day03$ echo $var
hello world
環境變量
登錄進程稱爲父進程
shell中執行的用戶進程均稱爲子進程
環境變量用於所有的用戶進程(稱爲子進程)
本地變量只用於現在的shell,環境變量用於所有的子進程,包括編輯器、腳本、應用程序
環境變量的設置
cat $HOME/.profile
環境變量對所有的用戶進程都有效
聲明環境變量 export var="helloworld"
查看環境變量 env
心裏要有shell 心裏要有Linux內核
shell中的反引號、單引號、雙引號
雙引號會引用變量的值
hzmct@U-64:/study/linuxtest/day03$ echo -e "chenlei $SHELL"
chenlei /bin/bash
單引號不做任何解釋,直接輸出
hzmct@U-64:/study/linuxtest/day03$ echo -e 'chenlei $SHELL'
chenlei $SHELL
反引號,裏面的東西是執行命令
hzmct@U-64:/study/linuxtest/day03$ test=`date`
hzmct@U-64:/study/linuxtest/day03$ echo $test
2018年 08月 29日 星期三 15:34:09 CST
特殊字符
雙引號(“”):用來使shell無法認出空格、製表符和其他大多數特殊字符,比如:建立一個帶空格的文件touch “wang bao ming”。
單引號(‘):用來使shell無法認出所有特殊字符。
反引號(`):用來替換命令
反斜槓(\):用來使shell無法認出的特殊字符,使其後的字符失去了特殊的含義,轉義字符。eg:創建帶空格文件名 touch my\ file
分號(;):允許在一行上放多個命令。mv 1.txt 2.txt; mv 2.txt 3.txt;
&:命令後臺執行
括號():創建成組的命令
大括號{}:創建命令塊。
豎槓(|):管道標示符
< > &:重定向表示符
* ? [ ] !:表示模式匹配
$:變量名的開頭
空格、製表符、換行符:當做空白
變量替換
用變量的值替換它的名字
echo
在變量名前加$,使用echo命令可以顯示單個變量取值。
echo ${MYVAR} 或者echo $MYVAR
定義本地變量
$ var4=
若設置了變量,則顯示變量的值,否則顯示新值
$ echo ${var4:?}
1234
若設置了變量,則顯示新值,否則顯示爲空
$ echo ${var4:+}
meu
若設置了變量,則顯示變量的值,否則顯示新值
$ echo ${var4:-}
1234
若設置了變量,則顯示變量的值,否則顯示新值
$ echo ${var4:=}
1234
//清除變量
readonly的變量,不能被清除
$ unset var4
$ echo ${var4:?}
-bash: var4: meiyou
$ echo ${var4:+}
$ echo ${var4:-}
33
$ echo ${var4:=}
44
位置變量
hzmct@U-64:/study/linuxtest/day03$ cat weizhibianliang.sh
例1:
#!/bin/bash
echo "腳本的名字 $0"
echo "parm 1 $1 "
echo "parm 2 $2 "
echo "parm 3 $3 "
echo "parm 4 $4 "
echo "parm 5 $5 "
shift 2
echo "Here's the new first parameter: $1"
echo "顯示參數的個數: $#"
echo "顯示腳本的全部參數: $*"
echo "腳本運行的當前進程ID號: $$"
echo "顯示前一命令運行後狀態: $?"
執行腳本
hzmct@U-64:/study/linuxtest/day03$ ./weizhibianliang.sh 11 22 33 44 55
腳本的名字 ./weizhibianliang.sh
parm 1 11
parm 2 22
parm 3 33
parm 4 44
parm 5 55
Here's the new first parameter: 33
顯示參數的個數: 3
顯示腳本的全部參數: 33 44 55
腳本運行的當前進程ID號: 2470
顯示前一命令運行後狀態: 0
例子2
hzmct@U-64:/study/linuxtest/day03$ cat find.sh
#!/bin/bash
#使用方法./find.sh "*.sh"
find ./ -name "$1" -print
執行腳本
hzmct@U-64:/study/linuxtest/day03$ ./find.sh "*.sh"
./testdir1.sh
./weizhibianliang.sh
./weizhi_.sh
./find.sh
./testdir.sh
./read.sh
./readonline.sh
./1.sh
./exec.sh
標準變量
$ echo $HOME
/home/hzmct
$ echo $LOGNAME
hzmct
$ echo $TERM
xterm
$ set | grep "PATH"
$ echo $MAIL
/var/mail/hzmct
$ echo $PS1
\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$
hzmct@U-64:/study/linuxtest/day03$ echo $PS2
>
hzmct@U-64:/study/linuxtest/day03$ ls -lt | \
> wc -l
13
hzmct@U-64:/study/linuxtest/day03$
特殊變量
$# 傳遞到腳本的參數個數
$* 以一個單字串顯示所有向腳步傳遞的參數,與位置變量不同,次選項參數可以超過9個。
$$ 腳步運行的當前進程ID號
$! 後臺運行的最後一個進程的進程ID
$@ 與$#相同,但是使用時加引號,並在引號中返回每個參數
$- 顯示shell使用的當前選項,與set命令功能相同
$? 顯示最後命令的退出狀態。0表示沒有錯誤,其他任何值表明有錯誤
影響變量的命令
declare
hzmct@U-64:/study/linuxtest/day03$ help declare
-f restrict action or display to function names and definitions 只顯示函數名
-g create global variables when used in a shell function; otherwise ignored
-p display the attributes and value of each NAME
Options which set attributes:
-a to make NAMEs indexed arrays (if supported)
-A to make NAMEs associative arrays (if supported)
-i to make NAMEs have the `integer' attribute
-l to convert NAMEs to lower case on assignment
-n make NAME a reference to the variable named by its value
-r to make NAMEs readonly(設置變量只讀)
-t to make NAMEs have the `trace' attribute
-u to convert NAMEs to upper case on assignment
-x to make NAMEs export(聲明環境變量)
--------------------------示例----------------------------
$ declare -i num=12
$ declare -p num
declare -i num="12"
$ arr[0]=x
$ arr[1]=y
$ declare -a arr[2]=z
查看數組
$ echo ${arr}
x
$ echo ${arr[2]}
z
$ echo ${arr[8]}
$ declare -x t=123
$ declare -r t=123
$ t=55
-bash: t: readonly variable
$ declare -p
$ export AA="12345"
$ declare -p AA
declare -x AA="12345"
export
export: export [-fn] [name[=value] ...] or export -p
Options:
-f refer to shell functions
-n remove the export property from each NAME
-p display a list of all exported variables and functions
An argument of `
export 用戶創建傳給shell的變量,即:創建環境變量
-f 表明在“名-值”對中的名字是函數名。
-n 把全局變量轉換成局部變量。換句話說,命令的變量不在傳給shell
-p 顯示全局變量列表
hzmct@U-64:/study/linuxtest/day03$ var2="i am global now"
hzmct@U-64:/study/linuxtest/day03$ export var2
hzmct@U-64:/study/linuxtest/day03$ sh
$ echo $var2
i am global now
$ exit
hzmct@U-64:/study/linuxtest/day03$ echo $var2
i am global now
hzmct@U-64:/study/linuxtest/day03$ export -n var2
hzmct@U-64:/study/linuxtest/day03$ sh
$ echo $var2
$ exit
hzmct@U-64:/study/linuxtest/day03$
readonly
readonly 用於顯示或者設置只讀變量
-- 表明選項結束
-f 創建只讀變量
set
set 使用set命令顯示所有本地定義的shell變量。
設置或者重設各種shell
set -a
env
env 查看所有環境變量。
shift
shift [n] 用於移動位置變量,調整位置變量,是$3的值賦予$2,$2的值賦予$1
typeset
typeset 用於顯示或設置變量
是declare的同義詞
unset
unset 用於取消變量的定義
-- 表明選項結束
-f 刪除只讀變量,但不能取消從shell環境中刪除指定的變量和函數。如:PATH,PS1,PS2,PPID,UID,EUID等的設置