您的位置:首页 > 博客中心 > 网络系统 >

Shell 编程

时间:2022-04-03 16:25

一、shell概述

shell是一个命令行解释器,它接收应用程序/用户命令,然后调用操作系统内核
技术图片

1.1linux提供的shell解析器有

      cat /etc/shells
      /bin/sh
      /bin/bash
      /sbin/nologin
      /bin/dash
      /bin/tcsh
      /bin/csh

1.2bash和sh的关系

      cd /bin
      ll | grep bash
      -rwxr-xr-x.   1 root root    964544 4月  11 2018 bash            
      lrwxrwxrwx.   1 root root         4 12月 29 14:52 sh -> bash
      显然看出sh是bash的软连接

1.3centos 默认的解析器是bash

`echo $SHELL`
`/bin/bash`

二、shell脚本入门

2.1脚本格式

 脚本以#!/bin/bash开头(指定解析器)

2.2第一个shell脚本:helloworld

1)创建一个shell脚本,输出helloworld

vi helloworld.sh

2)在helloworld.sh中输入如下内容

#!/bin/bash
echo "helloworld"

3)脚本的执行方式

第一种:采用bash或sh+脚本的相对路径或绝对路径(不用赋予脚本+x权限)
 bash /home/atguigu/datas/helloworld.sh
 bash helloworld.sh
 Helloworld
第二种:采用输入脚本的绝对路径或相对路径执行脚本(必须具有可执行权限+x)
 chmod +x helloworld.sh
 ./helloworld.sh
 /home/atguigu/datas/helloworld.sh
 Helloworld
 注意:第一种执行方法,本质是bash解析器帮你执行脚本,所以脚本本身不需要执行权限。第二种执行方法,本质是脚本需要自己执行,所以需要执行权限。

三、变量

本类排行

今日推荐

热门手游