Linux程序编程(一)

本文详细介绍了Linux Shell的双重含义及作用,讲解了系统调用与用户手动调用的两类Shell脚本,并通过实例展示了Shell脚本的基本语法,包括环境变量设置、条件判断、循环结构等。同时,文中提到了常用Shell命令解释器如bash,并阐述了它们在脚本执行中的角色。
摘要由CSDN通过智能技术生成

第一章 shell 脚本

1. shell 概述:

  • shell 的两层含义:既是一种应用程序,又是一种程序设计语言。

  • shell 是用户和 Linux 内核之间的接口程序

  • Linux 系统中提供了好几种不同的 shell 命令解释器,如 sh、ash、bash 等。一般默认使用 bash 作为默认的解释器。我们后面编写的 shell 脚本,都是由上述 shell 命令解释器解释执行的

image-20210122163658893

2. shell 脚本大体可以分为两类:

  • (1)系统进行调用:

  • 这类脚本无需用户调用,系统会在合适的时候调用,如:/etc/profile、~/.bashrc 等。
  • /etc/profile
此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行,系统的公共环境变量在这里设置开始自启动的程序,一般也在这里设置
  • ~/.bashrc
用户自己的家目录中的.bashrc登录时会自动调用,打开任意终端时也会自动调用这个文件一般设置与个人用户有关的环境变量,如交叉编译器的路径等等
  • (2)用户编写,需要手动调用的:

3. shell语法:

image-20210122164114219

第一个程序

#!/bin/bash
clear
echo "this is the first shell script"

image-20210122164524479

image-20210122164949417

image-20210122165027411

image-20210122165055641

image-20210122165104923

#!/bin/bash
echo "your shell script name is $0"
echo "the params of your input is $*"
echo "the num of your input params is $#"
echo "the params is $1 $2 $3 $4"

image-20210122165340881

#!/bin/bash
name=teacher
string1="good moring $name"
string2='good moring $name'
echo $string1
echo $string2
echo "today is `date` "
echo 'today is `date` '
echo -e "this \n is\ta\ntest"
( name=student;echo "1 $name" )
echo 1:$name
{ name=student; echo "2 $name"; }
echo 2:$name

image-20210122165551506

image-20210122165650201

image-20210122165703260

image-20210122165718189

image-20210122165729439

image-20210122165751565

image-20210122165805198

#!/bin/bash
echo "Press y to continue"
read yn
if [ $yn = "y" ]; then
echo "script is running..."
else
echo "stopped!"
fi

image-20210122165916723

image-20210122165929187

image-20210122165946587

image-20210122165956260

#!/bin/bash
declare -i sum
for (( i=1; i<=100; i=i+1 ))
do
	sum=sum+i
done
echo "The result is $sum"
#!/bin/bash
for i in 1 2 3 4 5 6 7 8 9
do
	echo $i
done

image-20210122170053758

image-20210122170108092

image-20210122170116843

image-20210122170130315

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不能say的秘密

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值