the basic shell skills.
 
  • Bourne shell
    • sh
    • ksh
    • Bash
    • psh
    • zsh
  • C shell
    • csh
    • tcsh
 
[root@bogon temp]# cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh
 
 
1.echo
 
[root@bogon temp]# echo "hello world!"
-bash: !": event not found
[root@bogon temp]# echo 'hello world!'
hello world!
 
 
2.sh
 
sh script must declare #!/bin/bash at the begining
you'd better show note 
 
[root@bogon sh]# cat -A hello.sh
#!/bin/bash$
#Ryan$
$
echo "Hello World"$
 
 
cat -A will print all character include \n. You can see, my script hava a $, this is the \n in linux, if in windows, it will be ^M$, you have to run dos2unix to convert it.
 
 
3.Basis
 
 
3.1.history
 
show the history command you had typed
history
!n --execute the num n
!!  --execute the laste , acturelly, we use ↑
 
3.2.alias
 
[root@bogon sh]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias vi='vim'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
 
 
3.3 shortcuts
 
ctrl+A     move to line start
ctrl+E     move to line end
ctrl+C     stop
ctrl+L     clean
ctrl+U   delete the command before the cursor
ctrl+K    delete the command after the cursor
ctrl+Y     paste the command that you ctrl+U/K
ctrl+R     search from history
ctrl+D     exit
ctrl+Z     suspend and background
 
 
 
4.command
 
ls -a /etc/ | more
 
grep 
-i ingnore case
-n show linenum
-v negate
 
var
  • =bothends can't be space
  • $var_name, ${varname}
  • `` ==$()
 
envrionment
parent-shell->sub-shell
export var_name=var_value
env
unset var_name
source profile, . ./profile
  • /etc/profile
  • /etc/profile.d/*.sh
  • ~/.bash_profile
  • ~/.bashrc
  • /etc/bashrc
 
 
 
 
? one
* any
[] the char inside[]
[-] the char in the range of [-]
[^] the  opposite char
'' anything is char
"" double quotation, specific char like "$"、"`"、"\"
`` ==$()
$() command to be var
# note
\ transfferred meaning
 
 
var position
 
$n $0-command itselt; $1-$9 the attr after command; beyond 10 ${10}
$* all attrs as a attr
$@ all attrs as attrs
$# the sum of attrs
$? the status of the last command : 0? correct:error
$$ current PID
$! last PID in backgroud
 
 
read -t 30 -p "please input somethind :"  sm
echo $sm
 
 
caculate num
  • declare -i cc=$aa+$bb
  • dd=$(expr $aa + $bb)
  • ff=$(($aa+$bb))
 
 
regular
 
* behind char any times
. one char
^ begin
$ end
[] any inside char
[^] can be inside char
\ transfer
\{n\} n times behind. [0-9]\{4\}  ~ 1234; [1][3-8][0-9]\{9\} ~ tel
\{n,\} at least n times
\{n,m\} at least n times and at most m times
.* any char any times
\.$ end with .
 
 
output
 
command >file         cover
command >>file       append
err command 2>file
err command2>>file
 
command>file 2>&1
command  &>> file
 
char cut
 
cut
-d seperator
-f the index
 
[root@00:09:13 /temp/project/sh]$ cut -d ":" -f 1,3 /etc/passwd
root:0
bin:1
 
[root@00:13:16 /temp/project/sh]$ cat /etc/passwd | grep /bin/bash | grep -v root | cut -d ":" -f 1,3
amandabackup:33
ryan:1000
yue:1001
yangmi:1002
bimm:1003
cangls:1004
st:1005
 
 
 
printf
%ns:      n char
%ni:       n num
%m.nf     12.34
 
 
awk
 awk [POSIX or GNU style options] -f progfile [--] file ...
 
 
[root@00:24:15 /temp/project/sh]$ cat stu.txt
name    age aa   
mrf    12    bb
ryan    18    vv
[root@00:24:31 /temp/project/sh]$ awk '{printf $2 "\t" $3 "\n"} ' stu.txt
age    aa
12    bb
18    vv
 
[root@00:24:33 /temp/project/sh]$ df -h | awk '{print $1 "\t" $5 "\t" $6}'
[root@00:29:04 /temp/project/sh]$ df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   50G  5.3G   45G   11% /
devtmpfs                 1.6G     0  1.6G    0% /dev
tmpfs                    1.6G     0  1.6G    0% /dev/shm
tmpfs                    1.6G  9.0M  1.6G    1% /run
tmpfs                    1.6G     0  1.6G    0% /sys/fs/cgroup
/dev/mapper/centos-home  278G   37M  278G    1% /home
/dev/sda6                497M  162M  335M   33% /boot
tmpfs                    327M     0  327M    0% /run/user/0
[root@00:29:12 /temp/project/sh]$ df -h | grep root | awk '{print $5}' | cut -d "%" -f111
 
 
 
 
sed
 
change the file line2
sed -i '2c theReplaceMsg'  file
 
p     printf
d     delte
a     append
 i     insert
c     replace   line
s     sed 'ns/old/new/g' file
 
 
if
 
[root@bogon sh]# [ "$aa"=="$bb" ] && echo yes || echo no
yes
 
-n var_name      if var_name!=null return true
-z  var_name      if var_name==null return true
! var_name     opposite  
 
 
if [    ]  ;then
  ....
elif [    ]
     then
     ...
else
     ...
fi
or
if [   ]
     then
.....
fi
 
     
case
 
case $var_name in
     val1)
          ...
          ;;
     val2)
          ...
          ;;
     *)
          ...
          ;;
 esac
 
 
for
 
for var_name in val1 val2 val3 ...
     do
          ..
     done
 
 
while
 
while [  $i -le 100    ]
     do
          ...
     done
 
 
 
 
5.Other
 
ps
 
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.1  41632  4060 ?        Ss   5月13   0:12 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
root         2  0.0  0.0      0     0 ?        S    5月13   0:00 [kthreadd]
root         3  0.0  0.0      0     0 ?        S    5月13   0:05 [ksoftirqd/0]
root         7  0.0  0.0      0     0 ?        S    5月13   0:00 [migration/0]
root         8  0.0  0.0      0     0 ?        S    5月13   0:00 [rcu_bh]
root         9  0.0  0.0      0     0 ?        S    5月13   0:00 [rcuob/0]
root        10  0.0  0.0      0     0 ?        S    5月13   0:00 [rcuob/1]
root        11  0.0  0.0      0     0 ?        S    5月13   0:00 [rcuob/2]
root        12  0.0  0.0      0     0 ?        S    5月13   0:00 [rcuob/3]
root        13  0.0  0.0      0     0 ?        S    5月13   4:18 [rcu_sched]
 
top
 
kill
 
kill -1  restart
kill -9  pid  (force)
kill 15  pid  (default)
 
 
crond
crontab -e
 
 
 

learn shell的更多相关文章

  1. linux shell中的单引号与双引号的区别(看完就不会有引号的疑问了)(转)

    tips: ============================= IFS - LINUX字段分隔符,内部字段分隔符 IFS(Internal Field Seperator)在Linux的she ...

  2. Shell脚本编程与文件系统修复

    导读 Linux基金会发起了LFCS认证(Linux 基金会认证系统管理员)Linux Foundation Certified Sysadmin,这是一个全新的认证体系,旨在让世界各地的人能够参与到 ...

  3. shell单引号双引号详解

    linux shell中的单引号与双引号的区别(看完就不会有引号的疑问了) " "(双引号)与 ' '(单引号)的区别    你在shell prompt(shell 提示)后面敲 ...

  4. Shell学习(二)Shell变量

    一.Shell变量 变量的定义 例子: my_job="Learn Shell" PS:变量名和等号之间不能有空格!!! 命名只能使用英文字母,数字和下划线,首个字符不能以数字开头 ...

  5. mongoDB & Nodejs 访问mongoDB (一)

    最近的毕设需要用到mongoDB数据库,又把它拿出来再学一学,下盘并不是很稳,所以做一些笔记,不然又忘啦. 安装 mongoDB & mongoVUE mongoDB: https://www ...

  6. Linux--Introduction and Basic commands(Part one)

    Welcome to Linux world! Introduction and Basic commands--Part one J.C 2018.3.11 Chapter 1 What Is Li ...

  7. Linux awk命令使用方法

    awk是linux上非常好用的文本处理工具,常用于指定列的处理,包括获取指定列的内容.根据指定列匹配关系输出等文本处理.本文主要描述awk命令的基本语法.正则表达式与操作符的使用.常用内置变量的含义和 ...

  8. 2019 DevOps 技术指南

    原文链接:https://hackernoon.com/the-2018-devops-roadmap-31588d8670cb 原文作者:javinpaul 翻译君:CODING 戴维奥普斯 写在前 ...

  9. Learn the shell

    learn the shell what is the shell? when we speak of the command line,we are really to the shell.Actu ...

随机推荐

  1. RadioGroup实现导航栏

    ​[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...

  2. SQLSERVER中的ALL、PERCENT、CUBE关键字、ROLLUP关键字和GROUPING函数

    SQLSERVER中的ALL.PERCENT.CUBE关键字.ROLLUP关键字和GROUPING函数 先来创建一个测试表 USE [tempdb] GO )) GO INSERT INTO [#te ...

  3. 公司内部培训AlwaysOn PPT分享

    公司内部培训AlwaysOn PPT分享 下载地址: http://files.cnblogs.com/files/lyhabc/alwayson.ppt

  4. 【数据结构】平衡二叉树—AVL树

    (百度百科)在计算机科学中,AVL树是最先发明的自平衡二叉查找树.在AVL树中任何节点的两个子树的高度最大差别为一,所以它也被称为高度平衡树.查找.插入和删除在平均和最坏情况下都是O(log n).增 ...

  5. Android编译过程中的碎碎念

    刷机不是用rom包吗?怎么可以使用fastboot flashall -w将*.img文件刷入呢? 在Mac上面可以参考这篇文章进行刷机.概括来说解释从官方下载rom包,解压后运行./flash-al ...

  6. ASP.NET Web API中的Controller

    虽然通过Visual Studio向导在ASP.NET Web API项目中创建的 Controller类型默认派生与抽象类型ApiController,但是ASP.NET Web API框架本身只要 ...

  7. SqlServer英文单词全字匹配

    环境:Vs2013+Sql Server2012 问题:现在数据库记录如下: Sentence列保存的是英文的句子,我现在想找出所有包含“I”(单词)的句子,如果我用 Sentence like '% ...

  8. Android开发学习之路-二维码学习

    这个月装逼有点少了,为什么呢,因为去考软件射鸡师了,快到儿童节了,赶紧写篇博纪念一下逝去的青春,唔,请忽略这句话. 二维码其实有很多种,但是我们常见的微信使用的是一种叫做QRCode的二维码,像下面这 ...

  9. 高德地图-搜索服务-POI搜索

    高德地图-搜索服务-POI搜索 之前公司项目收货地址仿饿了么的收货地址,结果发现自己实现的关键字搜索和周边搜索,搜索到的poi列表跟饿了么的并不完全一样,后来考虑了下,应该是搜索的范围.类型之类的设置 ...

  10. c#属性中的get和set属性

    get是给属性赋值,set是取属性的值. get.set用法: 一是隐藏组件或类内部的真是成员: 二是用来建立约束的,比如,实现“有我没你”这种约束: 三是用来响应属性变化事件,当属性变化是做某事,只 ...