Linux Shell脚本教程

时间:2023-03-09 06:43:21
Linux Shell脚本教程

v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}

Normal
0
false

7.8 磅
0
2

false
false
false

EN-US
ZH-CN
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-qformat:yes;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-font-kerning:1.0pt;}

Linux Shell脚本教程

.背景

单纯的PHP也可以实现断点续传下载,但单纯的PHP实现代码会比较复杂,而且没有多线程下载,速度会比较慢。所以放弃PHP下载的方案,用linux下的axel命令实现。

.shell教程

1. 变量定义。

使用变量,最好都加 {} 边界符

2. 注释

没有多行注释,只能每一行加一个#号。但可以通过其它变通的方法实行多行注释,在编辑器不会着色,有兴趣的自行搜索。

3. 调用linux命令

不需要返回直接调用的:cd ${path}/cdn_log_download/echo/;rm
-f $f_name

有返回的:del_month=`date
+"%Y-%m" -d "-2month"`

使用命令实现算术运算:length=`expr
${#file_name_arr[*]} - 1`

使用命令实现关系运算:$val -eq
0

使用tr 命令分割字符:($(echo
$download_list | tr "||" "\n"))

 例子:

 将所以上上个月下载日志文件删除

del_list=`cd
/home/domaininfo/cdn_log_download/echo/;ls --full-time | awk '{ print $6,$9 }'
| grep "2015-12"`

 通道符:|

awk

grep:grep –rn “echo” *

4. 调用PHP,并传参

download_list=`cd
${path};/usr/bin/php cdn_log_download.php 'get_list'` 

5. 数组

定义多个数组,可以通过空格和换行符来实现

读取数组:f_name=${del_arr[$i]}

获取数组长度:${#del_arr[*]}

6. while循环

Linux Shell脚本教程

7. if else语句

8. break和continue命令

9. 判断文件夹或文件是否存在

if [ ! -f "$echo_path" ]

if [ ! -d "$save_file_path" ]

10. 输出重定向

$ who > users 覆盖

$ who >> users 追加

11. 文件名定义和执行

xxx.sh

./xxx.sh

********************************* 2016-07-06 **********************************1. 提示没有权限
chmod +x test.sh

Normal
0

7.8 磅
0
2

false
false
false

EN-US
ZH-CN
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-qformat:yes;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:等线;}