shell脚本(1)-shell介绍

一、shell介绍

1、什么是shell

shell是c语法编写的,是用户和liunx内核沟通的桥梁,即是命令语法,也是解释性编程语言。

内核处理的都是二进制,而用户处理的都是高级语法,系统开发人员可以通过shell快速操作计算机。

 

二、shell功能

1、解析你的命令

[root@localhost ~]# echo "helloword"
helloword

2、启动程序

[root@localhost ~]# redis-server /myredis/redis.conf 
[root@localhost ~]# ps -ef | grep redis
root       2111      1  0 01:13 ?        00:00:28 redis-server *:6379
root       2501   2469  0 05:53 pts/0    00:00:00 grep --color=auto redis

3、输入输出重定向

[root@localhost ~]# wc -c < testmail.txt 
27
[root@localhost ~]# echo "helloworld" >>  test0716.txt

4、管道连接

[root@localhost ~]# echo "hello world" | sed 's/world/亲/'
hello 亲

5、文件名置换(echo /*)

[root@localhost ~]# echo ${user-root}
root
[root@localhost ~]# user="bktest"
[root@localhost ~]# echo $? #判断上一条命令是否执行成,0代表成功
0

6、变量维护

复制代码
[root@localhost ~]# echo $USER #当前用户
root

[root@localhost ~]# echo $USER #当前用户
root

 
复制代码

7、环境控制

[root@localhost ~]# echo $HOME #用户登录的目录
/root
[root@localhost ~]# echo $UID #用户标识
0

8、shell编程

复制代码
#!/bin/sh

seconds_left=15

echo "请等待${seconds_left}秒……"
while [ $seconds_left -gt 0 ];do
  echo -n $seconds_left
  sleep 1
  seconds_left=$(($seconds_left - 1))
  echo -ne "\r     \r" #清除本行文字
done
echo "done!"
复制代码

输出倒计时15秒

 

三、shell脚本精髓

 shell脚本就是将完成一个任务的所有命令按照执行的先后顺序,自上而下写入一个文本文件中,然后给予执行权限。

举例示范:

[root@localhost test0717]# vim nginx_intall.sh #写入shell脚本
复制代码
#!/usr/bin/bash
#下载依赖 yum
-y install wget gcc pcre-devel zlib-devel
#下载nginx wget http:
//nginx.org/download/nginx-1.16.0.tar.gz
#解压缩 tar xf nginx-1.16.0.tar.gz
#进入目录 cd nginx
-1.16.0
#配置安装
./configure --prefix=/usr/local/nginx make make install
复制代码
[root@localhost test0717]# chmod +x nginx_intall.sh
[root@localhost test0717]# ./nginx_intall.sh

nginx安装成功,验证并启动

复制代码
... ... 
cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf.default'
test -d '/usr/local/nginx/logs' \
    || mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/logs' \
    || mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/html' \
    || cp -R html '/usr/local/nginx'
test -d '/usr/local/nginx/logs' \
    || mkdir -p '/usr/local/nginx/logs'
make[1]: Leaving directory `/root/test0717/nginx-1.16.0'
[root@localhost test0717]# cd /usr/local/nginx/
[root@localhost nginx]# ls
conf  html  logs  sbin
[root@localhost nginx]# ./sbin/nginx 
[root@localhost nginx]# ps -ef|grep nginx
root     109211      1  0 06:41 ?        00:00:00 nginx: master process ./sbin/nginx
nobody   109212 109211  0 06:41 ?        00:00:00 nginx: worker process
root     109214 103997  0 06:41 pts/0    00:00:00 grep --color=auto nginx
复制代码

 

posted @   Mrwhite86  阅读(123)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示