文章目录
1. shell 脚本语言
首行指定解释器:#!/bin/bash
特殊字符的含义
shell 控制流
if 循环
if condition1
then
command1
elif condition2
then
command2
else
commandN
fi
附:
1、如果else分支没有语句执行,就不要写这个else
2、末尾的fi就是if倒过来拼写,后面还会遇到类似的
写成一行(适用于终端命令提示符):if [ $(ps -ef | grep -c "ssh") -gt 1 ]; then echo "true"; fi
例子:
#!/bin/bash
a=1
b=2
if [[ $1==a && $2==b ]]
then
echo 'This a test shell script.'
else
echo 'Please give some paras.'
fi
输出:
This a test shell script.
for 循环
for var in item1 item2 ... itemN
do
command1
command2
...
commandN
done
写成一行:for var in item1 item2 ... itemN; do command1; command2… done;
注:in列表是可选的,如果不用它,for循环使用命令行的位置参数,比如:
for loop in 1 2 3 4 5
do
echo "The value is: $loop"
done
输出:
The value is: 1
The value is: 2
The value is: 3
The value is: 4
The value is: 5
while 语句
while condition
do
command
done
2. 实操
2.1 shell基本用法
新建shell脚本文件:vim test.sh
注意,新建shell文件之后,需要:
- 赋予脚本执行权限:
chmod +x ./test.sh - 首行需要指定编辑器:
#!/bin/bash
运行 shell:./test.sh
步骤:(注意shell脚本中第一行代码要指定编辑器)
-
在终端 cd 到相应目录
-
使.sh脚本具有执行权限:
chmod +x ./test.sh -
在终端执行脚本:
./test.sh
注意,一定要写成 ./test.sh,而不是 test.sh,运行其它二进制的程序也一样,直接写 test.sh,linux 系统会去 PATH 里寻找有没有叫 test.sh 的,而只有 /bin, /sbin, /usr/bin,/usr/sbin 等在 PATH 里,你的当前目录通常不在 PATH 里,所以写成 test.sh 是会找不到命令的,要用 ./test.sh 告诉系统说,就在当前目录找。
附:
chmod +x的意思是(Linux下)将文件由普通文件(灰色)变为可执行文件(绿色);
chmod -x的含义相反!
#!/bin/bash
echo 'This a test shell script.'

传递参数
#!/bin/bash
echo $0
echo $1
echo $2
echo 'This a test shell script.'
-
不传递

-
传递
$0、$1和$2(即执行的文件名、第1个参数和第2个参数)
(这里多输入了一个参数!)
小结:
1、脚本内获取参数的格式为:$n。n 代表一个数字,1 为执行脚本的第一个参数,2 为执行脚本的第二个参数,以此类推……
2、其它
$# 传递到脚本的参数个数
$* 以一个单字符串显示所有向脚本传递的参数。
如"$*"用「"」括起来的情况、以"$1 $2 … $n"的形式输出所有参数。
$$ 脚本运行的当前进程ID号
$! 后台运行的最后一个进程的ID号
$@ 与$*相同,但是使用时加引号,并在引号中返回每个参数。
如"$@"用「"」括起来的情况、以"$1" "$2" … "$n" 的形式输出所有参数。
$- 显示Shell使用的当前选项,与set命令功能相同。
$? 显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误。
2.2 实际应用(Python和shell交互使用)
2.2.1 shell 脚本运行Python文件:直接在shell脚本中写入 ./xxx.py
具体步骤:
-
保证python脚本文件中已经指定了解释器:
#!/Users/robin/software/anaconda3/bin/python(根据自己的来——可以使用which python确定)如果在linux系统上出现类似
import: unable to open X server‘ @
error/import.c/ImportImageCommand/364.的报错,可以使用#!/usr/bin/env
python3,`详细可以查看:在shell中执行Python脚本出错 -
使.py脚本具有执行权限:
chmod +x python_test.py -
运行脚本:
./python_test.py
其实最后一步里,像在命令行上运行一样加上python也是可以的:python ./python_test.py
附代码:
- python_test.py
#!/Users/robin/software/anaconda3/bin/python
for i in range(4):
print(i)
print('This is a test for python script')
- test.sh
#!/bin/bash
./python_test.py
结果:

2.2.2 命令行运行Python文件:python xxx.py
这个直接在命令行运行 pyhon xxx.py 即可!
(先确保该Python脚本是可执行文件)
结果:

2.2.3 命令行运行带参数的Python脚本:xxx.py arg1 arg2 …
有几种方法可以实现:sys.argv 模块、argparse 模块和第三方的 click 模块等。
以下使用sys.argv 模块,执行系统为Mac OS。
#!/Users/robin/software/anaconda3/bin/python
import sys
def python_argument():
args = sys.argv
n = 1
for i in args:
print('The NO.{} arg is {}'.format(n,i))
n += 1
python_argument()
print('Well done!')
命令行执行及结果:

2.2.4 shell脚本运行带参数的Python脚本:./xxx.py arg1 arg2 arg3 …
这个只需要 Python脚本内调用好sys.argv 等模块,使用上面的方法就可以了!
2.2.5 Python调用shell脚本
有三种方法可以实现:os.system方法、os.popen方法和subprocess模块,这里主要讲os.system方法,更多可以参考:正确的使用python调用shell的姿势
感觉使用起来并没有多方便,暂时放弃!
主要参考:
2129



被折叠的 条评论
为什么被折叠?



