linux命令学习(一)——shell基础及工具

学习资料:《linux大棚命令百篇上》

这学期选了linux基础与应用选修课,在图书馆偶然看到这本书,翻了一下觉得还可以,语言比较有特色。由于linux命令知识很散,今天看了可能过几天就忘了,用到了又要回头找,于是准备将各种命令的用法记录下来。

  • export
  • read
  • expr
  • alias
  • history
  • time
  • sleep

export###

  1. 展示shell环境变量
wangsheng@ubuntu[20:37:40]:~$ export
declare -x CLUTTER_IM_MODULE="xim"
declare -x COMPIZ_BIN_PATH="/usr/bin/"
declare -x COMPIZ_CONFIG_PROFILE="ubuntu"
declare -x DBUS_SESSION_BUS_ADDRESS="unix:abstract=/tmp/dbus-rrcxRMGTqK"
declare -x DEFAULTS_PATH="/usr/share/gconf/ubuntu.default.path"
declare -x DESKTOP_SESSION="ubuntu"
declare -x DISPLAY=":0"
declare -x GDMSESSION="ubuntu"
declare -x GDM_LANG="en_US"
declare -x GNOME_DESKTOP_SESSION_ID="this-is-deprecated"
declare -x GNOME_KEYRING_CONTROL=""
declare -x GNOME_KEYRING_PID=""
declare -x GPG_AGENT_INFO="/home/wangsheng/.gnupg/S.gpg-agent:0:1"
declare -x GTK2_MODULES="overlay-scrollbar"
declare -x GTK_IM_MODULE="ibus"
......
  1. export定义环境变量
wangsheng@ubuntu[20:36:38]:~$ a=1
wangsheng@ubuntu[20:36:49]:~$ export b=2
wangsheng@ubuntu[20:36:57]:~$ echo $a
1
wangsheng@ubuntu[20:37:11]:~$ echo $b
2
wangsheng@ubuntu[20:37:15]:~$ bash
wangsheng@ubuntu[20:37:18]:~$ echo $a
wangsheng@ubuntu[20:37:35]:~$ echo $b
2

定义变量a,全局变量b,使用bash命令创建一个新的子进程,在子进程中a未定义,b仍然存在。(退出子进程用exit命令)

  1. unset取消环境变量定义
wangsheng@ubuntu[20:39:58]:~$ unset b
wangsheng@ubuntu[20:42:57]:~$ echo $b

重新打印b的值的时候,已经没有输出了。


read###

  1. read接收输入,并把信息存放到变量中。
    使用此功能实现第一个脚本,用来接收输入的名字,并显示出来。创建一个文件firstscript.sh,文件后缀为.sh,使用vim打开文件,添加如下内容并保存:
#! bin/bash
echo -n "please tell me your name:"
read name
echo "welcome!!! ${name}"
exit 0

使用sh命令执行脚本

wangsheng@ubuntu[21:12:37]:~/Documents$ sh firstscript.sh 
please tell me your name:wangsheng
welcome!!! wangsheng

可以看到read命令读取键盘输入,并将结果存入到了name变量之中。

  1. read读取多个值。read可以同时接受多个值,使用空格分开。
    修改脚本如下:
#! bin/bash
echo -n "please tell me your name age sex:"
read name age sex
echo "welcome!!! ${name},age:${age},sex:${sex}"
exit 0

执行脚本:

wangsheng@ubuntu[21:28:27]:~/Documents$ sh firstscript.sh 
please tell me your name age sex:wangsheng 20 man
welcome!!! wangsheng,age:20,sex:man

1.当输入数据数量小于变量个数时,多余的变量就不会获取到数据,变量值为空。
2.当输入数据数量大于变量个数时,超出部分会赋值给最后一个变量。

  1. read -t 指定等待时间
read -t 5 name
  1. read -s 输入密码(输入字符不会回显)
read -s 5 name

expr###

  1. expr计算表达式
wangsheng@ubuntu[21:59:47]:~/Documents$ a=9;b=3
wangsheng@ubuntu[22:00:01]:~/Documents$ expr $a + $b
12
wangsheng@ubuntu[22:00:07]:~/Documents$ expr $a - $b
6
wangsheng@ubuntu[22:00:16]:~/Documents$ expr $a / $b
3
wangsheng@ubuntu[22:00:27]:~/Documents$ expr $a \* $b
27
wangsheng@ubuntu[22:00:39]:~/Documents$ expr $a * $b
expr: syntax error

注意×运算的时候要使用转义字符。

  1. expr进行字符串运算
    匹配字符串长度,找不到返回0(第二个参数是正则表达式)
wangsheng@ubuntu[22:05:15]:~/Documents$ expr match "123 456 789" ".*5"
6

从指定位置抓取字符串(第二个参数是起始下标,第三个参数是截取长度)

wangsheng@ubuntu[22:08:33]:~/Documents$ expr substr "hello world!" 1 5
hello

查找子字符串位置(第二个参数是子字符串)

wangsheng@ubuntu[22:08:39]:~/Documents$ expr index "hello world!" "o"
5

计算子字符串长度

wangsheng@ubuntu[22:10:24]:~/Documents$ expr length "hello world!"
12

alias###

  1. alias定义别名。alias 别名='原命令'
wangsheng@ubuntu[22:13:40]:~/Documents$ alias la='ls -a'
wangsheng@ubuntu[22:14:03]:~/Documents$ la
.   file.sh         name       readfile.sh  writefile.sh
..  firstscript.sh  namescore  score
  1. alias查看所有别名
wangsheng@ubuntu[22:24:52]:~$ alias
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -a'
alias ll='ls -alF'
alias ls='ls --color=auto'
  1. alias查看某个别名(alias 别名)
wangsheng@ubuntu[22:14:18]:~$ alias la
alias la='ls -a'
  1. unalias取消别名(unalias 别名)
wangsheng@ubuntu[22:19:36]:~$ unalias la
wangsheng@ubuntu[22:21:30]:~$ alias
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias ll='ls -alF'
alias ls='ls --color=auto'

取消la后再查看所有别名就没有la了

  1. 别名与命令冲突时,如何区分执行的是别名对应的命令还是命令本身
wangsheng@ubuntu[22:28:16]:~$ alias ls='ls -a'

执行命令本身

wangsheng@ubuntu[22:29:11]:~/Documents$ \ls
file.sh     name       readfile.sh  writefile.sh
firstscript.sh  namescore  score

执行别名命令

wangsheng@ubuntu[22:29:15]:~/Documents$ ls
.   file.sh     name       readfile.sh  writefile.sh
..  firstscript.sh  namescore  score

history###

  1. history列出已经输入过的命令
wangsheng@ubuntu[22:29:20]:~/Documents$ history
    1  sudo
    2  rot
    3  root
    4  sudo vi /etc/apt/sources.list
    5  sudo apt-get update
    6  sudo apt-get upgrade
    7  gredit

更改HISTTIMEFORMAT环境变量设置history显示时间

wangsheng@ubuntu[22:38:33]:~/Documents$ export HISTTIMEFORMAT="%F %T   "
wangsheng@ubuntu[22:38:51]:~/Documents$ history 
    1  2017-03-24 20:37:18   sudo
    2  2017-03-24 20:37:18   rot
    3  2017-03-24 20:37:18   root
    4  2017-03-24 20:37:18   sudo vi /etc/apt/sources.list
    5  2017-03-24 20:37:18   sudo apt-get update
    6  2017-03-24 20:37:18   sudo apt-get upgrade
  1. 执行某条命令(!行数)
wangsheng@ubuntu[22:42:36]:~/Documents$ !1
sudo
usage: sudo -h | -K | -k | -V
usage: sudo -v [-AknS] [-g group] [-h host] [-p prompt] [-u user]
usage: sudo -l [-AknS] [-g group] [-h host] [-p prompt] [-U user]
            [-u user] [command]
usage: sudo [-AbEHknPS] [-r role] [-t type] [-C num] [-g group] [-h
            host] [-p prompt] [-u user] [VAR=value] [-i|-s]
            [<command>]
usage: sudo -e [-AknS] [-r role] [-t type] [-C num] [-g group] [-h
            host] [-p prompt] [-u user] file ...

执行了history列表第1行的命令

  1. history -c清除所有历史记录
wangsheng@ubuntu[22:47:11]:~/Documents$ history -c
wangsheng@ubuntu[22:47:56]:~/Documents$ history 
   97  2017-03-24 22:48:00   history 

time###

  1. time用来显示命令执行的时间(time 命令)
wangsheng@ubuntu[09:02:55]:~/Documents$ time cat firstscript.sh 
#! bin/bash
echo -n "please tell me your name:"
read -t 5 name age sex
echo "welcome!!! ${name},age:${age},sex:${sex}"
exit 0
real    0m0.444s
user    0m0.000s
sys 0m0.000s

time分三部分real、user、sys
real:命令从开始执行到完成所花费的总时间,包括进程等待时间
user:进程执行用户态代码花费的时间,命令实际花费的时间,不包括等待阻塞时间
sys:进程在内核态运行的时间


sleep###

  1. sleep睡眠(sleep 秒数)
wangsheng@ubuntu[09:03:22]:~/Documents$ date;sleep 5s;date
Sat Mar 25 09:11:15 CST 2017
Sat Mar 25 09:11:20 CST 2017

sleep默认睡眠时间为秒,也可以指定其他的单位

  • s:秒
  • m:分钟
  • h:小时
  • d:天
wangsheng@ubuntu[09:11:20]:~/Documents$ date;sleep 1m 2s;date
Sat Mar 25 09:14:52 CST 2017
Sat Mar 25 09:15:54 CST 2017
  1. sleep毫秒级睡眠(sleep 小数)
wangsheng@ubuntu[09:15:54]:~/Documents$ time sleep 0.03
real    0m0.037s
user    0m0.000s
sys 0m0.000s

注意:sleep过程不占用CPU资源

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,738评论 5 472
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,377评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,774评论 0 333
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,032评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,015评论 5 361
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,239评论 1 278
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,724评论 3 393
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,374评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,508评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,410评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,457评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,132评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,733评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,804评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,022评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,515评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,116评论 2 341

推荐阅读更多精彩内容