第一章 shell 脚本
1. shell 概述:
-
shell 的两层含义:既是一种应用程序,又是一种程序设计语言。
-
shell 是用户和 Linux 内核之间的接口程序
-
Linux 系统中提供了好几种不同的 shell 命令解释器,如 sh、ash、bash 等。一般默认使用 bash 作为默认的解释器。我们后面编写的 shell 脚本,都是由上述 shell 命令解释器解释执行的
2. shell 脚本大体可以分为两类:
-
(1)系统进行调用:
-
这类脚本无需用户调用,系统会在合适的时候调用,如:/etc/profile、~/.bashrc 等。
-
/etc/profile
此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行,系统的公共环境变量在这里设置开始自启动的程序,一般也在这里设置
用户自己的家目录中的.bashrc登录时会自动调用,打开任意终端时也会自动调用这个文件一般设置与个人用户有关的环境变量,如交叉编译器的路径等等
3. shell语法:
第一个程序
#!/bin/bash
clear
echo "this is the first shell script"
#!/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"
#!/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
#!/bin/bash
echo "Press y to continue"
read yn
if [ $yn = "y" ]; then
echo "script is running..."
else
echo "stopped!"
fi
#!/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