learn shell

来源:互联网 发布:关之琳 知乎 编辑:程序博客网 时间:2024/06/06 02:53
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
 
 
 
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 堡垒之夜草变色怎么办 火柴没有擦的了怎么办 乙醚倒进下水道了怎么办 乙醚和水不分层怎么办 乙醚闻多了头晕怎么办 爱乐维吃了便秘怎么办 刮完滑石粉墙面很软怎么办 被硫酸泼到皮肤怎么办 头磕了一下头晕怎么办 家里有事与工作不能请怎么办 撞了头头晕想吐怎么办 猫不小心摔一下怎么办 一氧化二氮中毒怎么办 电脑开机变慢了怎么办 怎么办抚顺韦德健身卡 预售健身卡合法吗怎么办 被浓硫酸泼到怎么办 婴儿误喝了生水怎么办 宝宝喝了生水拉肚子怎么办 因妈妈喝生水宝宝拉肚子怎么办 喝了几口生水怎么办 不小心吃到蟑螂怎么办 吃了有蛆的樱桃怎么办 不小心误食了蛆怎么办 吃了有蟑螂的汤怎么办 调节天平时指针向右怎么办 香薰蜡烛融化了怎么办 香薰蜡烛挂壁怎么办y 粗蜡烛只烧中间怎么办 紫薯馒头变绿怎么办 小孩手开水烫了怎么办 被油烫伤了怎么办才不留疤 烫伤水泡蹭破了怎么办 烧伤的水泡破了怎么办 烧伤后水泡破了怎么办 烫伤泡破了红肿怎么办 烧伤第二天水泡破了怎么办? 烧伤后换药特别疼怎么办 盐酸溅到皮肤上怎么办 磷性磷酸酶高440怎么办 浓硫酸沾到皮肤上怎么办