Linux - Shell编程基础

本文深入探讨了Shell编程的基础知识,包括Shell分类、工作步骤、基础语法、环境变量等核心概念,并提供了Shell脚本的执行方法及常用命令解释。通过实例展示了Shell脚本设计的实践应用,涵盖输入输出重定向、程序设计语法、变量访问与引用等方面,旨在帮助开发者高效地进行Shell编程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用户和操作系统之间的接口

这里写图片描述

Shell分类

这里写图片描述

Shell的双重角色

命令解释程序
Shell的工作步骤
打印提示符
得到命令行
解析命令
查找文件
准备参数
执行命令
独立的程序设计语言解释器
KISS (Keep It Small and Stupid)
可复用工具tools
重定向和管道

也称Shell script(Shell脚本)
是一组命令

#!/bin/sh

ls -al
touch aa
cp aa bb

Shell编程的基础知识
Linux环境
Linux命令
Shell程序结构

Shell脚本的执行方法

#!/bin/sh
# first.sh
# This file looks through all the files in the current
# directory for the string POSIX, and then displays those
# files to the standard output

for file in *
do
     if grep –q POSIX $file
     then
          more $file
     fi
done

exit 0
方法1
$ sh script_file 
方法2
chmod +x script_file (可选chown, chgrp)
./script_file
方法3
source script_file,或
.script_file

Shell启动文件

sh
/etc/profile        login shell, system wide
~/.profile         login shell
ENV
csh
/etc/csh.cshrc       always, system wide
/etc/csh.login       login shell, system wide
~/.cshrc            always
~/.login            login shell
~/.logout           logout shell
/etc/csh.logout      logout shell, system wide
tcsh
~/.tcshrc          login shell
bash
/etc/profile  ~/.bash_profile  ~/.bash_login  ~/.bash_profile
/etc/bash.bashrc  ~/.bashrc
BASH_ENV

Shell输入/输出重定向举例

>:输出重定向
$ ls –l > lsoutput.txt
 >>:追加
$ ps >> lsoutput.txt
出错输出重定向 (2>)
$ kill –HUP 1234 > killout.txt 2> error.txt
<:输入重定向
$ more < killout.txt

Shell程序设计的语法

Shell环境变量
这里写图片描述
Shell变量赋值

    $ salutation=Hello
$ echo $salutation
Hello
$ salutation="Yes Dear"
$ echo $salutation
Yes Dear
$ salutation=7+5
$ echo $salutation
7+5

Shell变量访问

% echo$PAGER”
% echo${PAGER}”
使用{}避免歧异
% temp_name=“haha”
% temp=“hehe”
% echo $temp
hehe
% echo $temp_name
haha
% echo ${temp}_name
hehe_name
% echo ${temp_name}
haha

参数变量和内部变量举例1
假设脚本名为myscript
如果执行./myscript foo bar baz,结果如何?

#!/bin/sh
salutation="Hello"
echo $salutation
echo "The program $0 is now runnning"
echo "The 1st & the 2nd parameters were $1 & $2"
echo $*
exit 0
$ ./myscript foo bar baz
Hello
The program ./myscript is now runnning
The 1st & the 2nd parameters were foo & bar
foo bar baz

参数变量和内部变量举例2
假设脚本名为var3.sh
执行sh ./var3.sh hello world earth,输入如何?

#!/bin/sh 
echo "I was called with $# parameters" 
echo "My name is $0" 
echo "My first parameter is $1"
echo "My second parameter is $2"
echo "All parameters are $@"
$ sh ./var3.sh hello world earth
I was called with 3 parameters 
My name is ./var3.sh 
My first parameter is hello 
My second parameter is world 
All parameters are hello world earth

参数变量和内部变量举例3

1  #!/bin/sh 
2  echo "What is your name?" 
3  read USER_NAME 
4  echo "Hello $USER_NAME" 
5  echo "File ${USER_NAME}_file will be created" 
6  touch "${USER_NAME}_file" 

代码说明
第5行使用USERNAMEfileshell使USER_NAME,而不是 USERNAMEfile6使{USER_NAME}_file”避免任何输入给$USER_NAME的空格

Shell变量引用

当包含一个或多个空格时使用”…”
variablevariable被单引号(‘….’)引用时,不会被替换
variable\便使来替换

Shell变量引用举例

#!/bin/sh
myvar="Hi there"
echo $myvar                
echo "$myvar"          
echo '$myvar'          
echo \$myvar           
echo Enter some text        
read myvar              
echo '$myvar' is $myvar        
exit 0
                                                  Output
#!/bin/sh
myvar="Hi there"
echo $myvar                        Hi there
echo "$myvar"          Hi there
echo '$myvar'			$myvar
echo \$myvar               $myvar
echo Enter some text            Enter some text
read myvar              Hello
echo '$myvar' is $myvar        $myvar is Hello
exit 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值