初识shell脚本编程

2018-01-07  本文已影响0人  其实我很dou

一. Shell基础

shell是链接用户和linux内核的一个命令解释程序, 常见shell包括bash(linux默认)以及其他

1. shell概述

#!/bin/bash
echo 'hello world';

2. 脚本执行方式

3. 别名设置

  1. 临时别名

    • alias vi=vim
    • alias mell='ls -l --color=auto'
    • 移除临时别名 unalias vi
  2. 永久别名, 修改配置文件

    • vi ~/.bashrc
    • source ~/.bashrc

4. 历史命令 history

配置文件 cat ~/.bash_history

5. 输入输出

linux标准输入是键盘, 标准输出设备是显示器

5.1. 输出重定向
  1. 覆盖式重定向 ls > 1.log
  2. 追加重定向 ls >> 2.log
  3. 标准错误输出 2>>和2>
ls xdy 2>>1.log
ls xdy &>>2.log 执行正确和错误都输出
ls &>>2.log
ls xdy >> 1.log 2>>2.log
5.2. 输入重定向

<< wc << abc

6. 管道符

  1. ; 表示顺序执行
    ls; cd /tmp; touch a;
  2. && 逻辑与
  3. || 逻辑或
ls && echo 'yes' || echo 'no'
abc && echo 'yes' || echo 'no'
  1. | 管道
    netstat -an | grep 22 | wc -l;

7. 通配符

  1. ? 一个字符
    • 多个字符
  2. [] 范围
aa=`ls`
 1012  echo $ss
 1013  echo $aa
 1014  echo "$aa\n"

ls=`head hello.sh `
 1019  echo ls
 1020  echo $ls
 1021  echo $ls + "\n"
  echo "$ls\n"
 1023  echo 'abc'
 1024  bb=$(ls)
 1025  echo $bb
 1026  echo "\n$bb\n"

二. 变量

1. 用户变量

a=123
b=2
a=$a+$b;(不要出现空格)
echo $a;  # a=123+2
c="$a"456
d=${c}789
# d=123+2456789

set -u 如果变量未定义, 则提示

[root@localhost xdy]# echo $g

[root@localhost xdy]# set -u
[root@localhost xdy]# echo $g
-bash: g: unbound variable
[root@localhost xdy]# 

2. 环境变量

[root@localhost ~]# echo $PATH
[root@localhost ~]# PATH="$PATH":/root/test
[root@localhost ~]# echo $PATH
......:/root/test
[root@localhost ~]# hello.sh 
[root@localhost ~]# echo $PS1
[\u@\h \W]\$
[root@localhost ~]# PS1=' [\u@\a \w]\$ '
 [root@ ~]# PS1=' [\u@\t \w]\$ '
 [root@14:20:38 ~]# PS1='[\u@\t \w]\$ '
[root@14:20:46 ~]# cd /usr/bin/
[root@14:21:19 /usr/bin]# 
ls
[root@localhost log]# ls \
> haha
[root@localhost log]# cat /etc/sysconfig/i18n 
LANG="zh_CN.UTF-8"
#!/bin/bash

a=20;
b=30;
c=$(( $a + $b))
echo $c;

d=$1;
e=$2;
echo $(( $d + $e))
# 调用时, 需要在命令后跟上两个参数,, 用于填充$d 和 $e
#!/bin/bash

echo $*;
echo $@;
echo $#;

echo "\n"
for i in "$*"
        do
                echo $i
        done

for y in "$@"
        do
                echo $y
        done

3. 预定义变量

ls && echo 'yes' || echo 'no'  :
    |-- 如果ls正确执行, $?为0, echo 'yes' 判断$? = 0, 输出yes
    |-- 如果ls没有正确执行, $?不为0, echo 'yes' 判断非0, 不执行, echo 'no'判断非0, 执行, 输出no

每一条命令都会返回一个状态值, 来标识自己有没有成功执行, 返回的值, 会被保存到$? 预定义变量中

4. read命令 用户输入命令

#!/bin/bash

read -p "please input your name" -t 30 name
echo $name

read -p "please input your password:" -s password
echo -e "\n"
echo $password

read -p "please input your sex[M/F]:" -n 1 sex
echo $sex

三. shell编程-运算符

1. 声明变量类型

declare [+/-][选项] 变量

[root@localhost test]# unset arr
[root@localhost test]# arr[1]=haha
[root@localhost test]# arr[2]=hehe
[root@localhost test]# declare -a arr[3]=heihei
[root@localhost test]# echo ${arr}

[root@localhost test]# echo ${arr[*]}
haha hehe heihei
[root@localhost test]# echo ${arr[2]}
hehe

[root@localhost test]# a=1
[root@localhost test]# b=2
[root@localhost test]# declare -i c
[root@localhost test]# c=$a+$b
[root@localhost test]# echo $c
3
[root@localhost test]# declare -p c
declare -i c="3"
[root@localhost test]# declare +i c
[root@localhost test]# echo $c
3
[root@localhost test]# declare -p c
declare -- c="3"

2. 数值运算

推荐$(( ))这种写法

[root@localhost test]# aa=11
[root@localhost test]# bb=22
[root@localhost test]# cc=$(expr $aa + $bb)
[root@localhost test]# echo $cc
33
[root@localhost test]# dd=$(($aa+$bb))
[root@localhost test]# echo $dd
33
[root@localhost test]# ee=$[$aa+$bb]
[root@localhost test]# echo ee
ee
[root@localhost test]# declare -p ee
declare -- ee="33"
[root@localhost test]# let ff=$aa+$bb
[root@localhost test]# echo $ff
33
[root@localhost test]# declare -p ff
declare -- ff="33"
[root@localhost test]# let gg=1&&0
-bash: 0: command not found
[root@localhost test]# let gg=1&0
[1] 2988
-bash: 0: command not found
[root@localhost test]# gg=$((1&&0))
[1]+  Done                    let gg=1
[root@localhost test]# echo $gg
0

3. 变量测试

[root@localhost test]# hh=${jj-3}
[root@localhost test]# echo $hh
3
如果jj没有设置过, 则hh等于3, 如果为空, 则hh为空, 如果为某个值, 则hh也为某个值

四. 环境变量配置文件

source 配置文件 使更改完的配置文件立即生效
. 文件 作用等同于source

/etc/profile (PATH, umask)
/etc/profile.d/*.sh (lang)
~/.bash_profile (将~/bin加入到环境变量)
~/.bashrc (别名)
/etc/bashrc (PS1)
创建文件后, 默认是
666 = rw-rw-rw-   
666-022 = 
    rw-rw-rw- - ----w--w- 
    = rw-r--r--
创建目录后
777-022 = 
    rwxrwxrwx - ----w--w- 
    = rwxr-xr-x
~/.bash_logout(退出登录时执行的脚本)
~/.bash_history(保留历史命令)

五. 正则表达式

正则表达式和通配符的区别

* 表示前一个字符出现0次或者多次  a*会通篇匹配, 必须是用aa* 才会匹配包含a的内容
. 表示除换行符外的任意一个字符 
^ 匹配行首任意一个字符 ^a
$ 匹配行尾  a$
[] 括号中任意一个字符 [a-zA-Z]
[^] 取反
\ 转义  \.
{n\} 前面字符恰巧出现n次
\{n,\} 大于n次
\{n, m\} 大于n, 小于m
[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\} 匹配日期
[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\} 匹配ip
上一篇 下一篇

猜你喜欢

热点阅读