码迷,mamicode.com
首页 > 系统相关 > 详细

shell编程

时间:2019-04-13 16:28:23      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:spi   sql   pre   bz2   bin   循环   top   his   nts   

SHELL 编程

shell 基础命令
#cd /home 进入 ‘/home‘ 目录
cd .. 返回上一级目录
cd../.. 返回上两级目录
ls 查看目录中文件
ls -a 显示隐藏文件
ls -l 显示详细信息
ls -lrt 按时间显示文件
pwd 显示工作目录
mkdir newfile 创建‘newfile‘目录
mv dir1 dir2 移动/重命名一个目录
rm -rf file1 删除‘feil1‘文件以及目录
cat file1 从第一个字节开始正向查看文件内容
head -2 file1 查看一个文件的前两行
more file1 查看一个长文件内容
tac file1 从最后一行开始反响查看一个文件的内容
tail -n 20 file1 查看最后20条记录;
grep str /tmp/test 在文件‘/tmp/test‘ 中查找‘str’
grep ^err /usr/local/greenplum/log/20190412.log 在 文件‘/usr/local/greenplum/log/20190412.log’中查找以‘err‘ 开始的
grep [0-9] /data/gpmaster/gpseg-1/postgresql.conf 在文件中查找包含数字的行
find / -name file1 从 / 开始 查找指定文件名的文件或者目录
find / -user username 查找属于用户‘username‘的文件目录
bzip2 file 压缩file 文件
bunzip2 file.bz2 解压文件
gzip file 压缩文件
gunzip file.gz 解压文件
tar -cvf test.tar file 将file 打包成 test.tar
yum -y install [package] 下载并安装一个rpm包
yum -y update 更新当前系统中安装的所有的rpm包
yum remove [package] 删除一个 rpm包
yum list 列出当前系统中安装的所有包
yum search [package] 在rpm 仓库中搜索软件包
yum clean [package]清楚缓存目录 /var/cache/yum 下的软件包
yum clean headers 删除所有头文件
yum clean all 删除所有的缓存包和头文件
su - 切换用户
shutdown -r now 当前重启
shutdown -h now 当前关机
top 罗列使用cpu资源最多的任务
pstree 以属性图显示程序
passwd 修改密码
df -h 查看磁盘

1、shell 入门
1、shell 脚本 以#!/bin/sh 开头 其中 #! 用来告诉系统后面的参数用于执行该文件的程序
一般编写完成后 需要 chmod +x filename.sh 这样才可以执行 ./filename 来运行
同时脚本文件 以.sh 为后缀
2、变量定义和引用
在SHELL中 所有的变了都是字符串组成同时不需要对变量声明;赋值给一个变量就可以。
#/bin/bash
RZ=‘www.ruozedata.com‘
DAT=date

        输出变量
        echo ${RZ}
        echo ${DATE}

        静态变量
        K=V ‘V‘ "V"

        动态变量
      K=`V`

        其中 =前后不可能有空格

        引用:

        $KA 
        ${K}A

        3、传递参数
        脚本获取参数格式 $0 $1 $2 $3 .... 其中 $1为传递的第一个参数 而$0接受的是 ./parameter.sh 这个文件
        #!/bin/bash

        echo $1
        echo $2
        echo "个数:$#"
        echo "PID: $$"

        #ps -ef |grep 5480
        #ps -ef |grep 5480|grep -v grep

4、数组--在shell中 用() 来表示的数组,数组元素之间用空格来分隔,由此 可以定义如下形式:
--数组可以存放多个值,shell 只支持一维组(不支持多维数组)

     array=(1,2,3,4,5,6)    -----=号不能有空格,必须紧挨这数组名和数组元素
     定义数组
     arr=(1,2,3,4,5)

     shell 是弱类型,他并不要所有数组元素类型必须相同;
     arr=(60,88,"https://blog.51cto.com/965726")

  #!/bin/bash

        arr=(ruoze jepson xingxing dashu xiaoshiqi xiaohai)

        echo ${arr[@]}
        echo ${arr[*]}
        echo ${arr[4]}
        echo {#arr[@]}

        # chmod +x array.sh   授权

5、if 判断
语法格式
if condition;then
statement(s)
fi
例子:
#if.sh
#!/bin/bash

     A="abc"
     B="jepson"

     if [ $a == $b ]; then
         echo "=="
    else

         echo "!="

    fi

==前后空格
[ $a == $b ]

6、循环
while 循环 当添加满足时 while 重复地执行一组语句, 当条件不满足时 就退出while 循环
#!/bin/bash

        i=1
        sum=0
        while (i <= 100)
        do
           ((sum +=i))
             ((i++))
        done
      echo "the test $sum"

    for 循环用法

    for ((exp1;exp2;exp3))
    do
      statements
    done
    -----其中  exp1、exp2、exp3 是表达式,其中 exp2是判断条件,for 循环依据 Exp2 的结果来决定是否继续席下次循环;
    statements 是 循环体语句,可以有一条,或者多条;
    do 和 done 是shell 中关键字;
    #!/bin/bash

    sum=0

    for ((i=1;i<=100;i++))

    do
       ((sum +=i))
    done

echo "then sum $sum"

i++ ==>>i=i+1

7、分割
vi spilt.sh
#!/bin/bash

S="ruoze,jepson,xingxing,dashu,xiaoshiqi,xiaohai"
OLD_IFS="$IFS"
IFS=","
arr=($S)
IFS="OLD_IFS"

for x in ${arr[*]}
do
echo $x
done

#chmod +x ./spilt.sh
#./spilt.sh
ruoze
jepson
xingxing
dashu
xiaoshiqi
xiaohai


8、awk
awk options program file
1、 -F fs 指定行中划分数据字段的字段分隔符;
2、-f programFile 从指定文件中读取代码数据;
3、 -v var=value 定义awk 程序一个变量以及默认值
$0代表整个文本行
$1代表文本行中的第一个数据字段,$2代表第二个字段,以此类推

$ cat test.txt
This is a test, this is the first line of the test.
This is a test, this is the second line of the test.
Hahahaha!
$?awk ‘{print $1}‘ test.txt
This
This
Hahahaha!

#vi awk.log
a,b,c
1,2,3
4,5,6
#cat awk.log |awk ‘{ print $1 }‘
a,b,c
1,2,3
4,5,6
#cat awk.log| awk -F ‘,‘ ‘{ print $1}‘
a
1
4
#cat awk.log| awk -F ‘,‘ ‘{ print $3}‘
c
3
6
#cat awk.log |awk -F"," ‘NR >1{ print $3}‘
3
6

9、替换

vi sed.log

a b c
1 2 3

sed -i ‘s/a/aa‘ sed.log 用于将 a 替换成 aa
cat sed.log
aa b c
1 2 3

#sed -i "s/b/w" sed.log
#cat sed.log
wbb b c
1 2 3

前面加
#sed -i "s/^/uuuu&/g" sed.log
cat sed.log
uuuwww w c
uuu1 2 3

后面加
#sed -i "s/$/&uuu/g" sed.log
cat sed.log
uuwww w cuuu
uuul 2 3uuu

shell编程

标签:spi   sql   pre   bz2   bin   循环   top   his   nts   

原文地址:https://blog.51cto.com/965726/2378225

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!