shell编程入门开始:

来源:互联网 发布:mac第一次充电多久 编辑:程序博客网 时间:2024/06/05 04:50

初衷: 通过shell脚本,一键搞定 文件创建,版本大包,文件上传,等一系列批处理指令,以前用java比较多, shell编程也至少需要的时候再到网上查看一下, 今天稍微系统看了一下,简单总结一下吧:   想法是,java中常用的功能 如何通过 shell 去实现:


#!/bin/bash#Define a functionfunction loopFile(){    count=0;    echo ${count};}loopFile#include params Functionfunction add(){sum_1=$1;sum_2=$2;echo ${sum_1}echo ${sum_2}echo $[${sum_1}*${sum_2}];}add 1 2for((i=0;i<10;i++ ));doecho ${i}donea=(1 2 3 4 5)echo ${#a[*]}           # #代表获取数组长度echo ${a[1]}            # 获取指定index对应的值echo ${a[*]}            # 获取数组中所有值a[1]=100                # 赋值echo ${a[*]}echo ${a[*]:0:3}        #分片操作  获取  下标  0-3的值a=(1,2,3,4,5)echo ${a[*]/3/500}       #将下表为3的值替换为 500a=(1 2 3 4 5)             #for循环遍历数组for i in ${a}doecho ${a[i]}doneString="helloWorld"        #遍历字符串中的每一个字符for((i=0;i<${#String};i++))doecho ${String:i:1}doneFile="/debuger.sh"          #for循环遍历文件内容: < 将文件内容重定向到变量中Current=$(pwd)FilePath=${Current}${File}echo ${FilePath}for line in  $(<${FilePath})doecho ${line}doneFile="/debuger.sh"          #for循环遍历文件内容:  > 写文件  ; < 读文件Current=$(pwd)FilePath=${Current}${File}cat ${FilePath} | while read line;doecho ${line}doneFile="/debuger.sh"          #for循环遍历文件内容:  > 写文件  ; < 读文件Current=$(pwd)FilePath=${Current}${File}cat ${FilePath} | while read line;doecho ${line}doneFile="/text.txt"              # -e 允许一行执行多行命令  d参数 删除读入缓冲区对应行字符File2="/text1.txt"Current=$(pwd)FilePath=${Current}${File}FilePath2=${Current}${File2}if [ -f ${FilePath2} ]; then    rm ${FilePath2}fised -e '1,2d' ${FilePath} > ${FilePath2}            #删除第一行到第二行 并重定向到新创建的文件File="/text.txt"              #  s 替换指定输入流中第一次出现的字符为指定字符 并指定到输出文件File2="/text1.txt"Current=$(pwd)FilePath=${Current}${File}FilePath2=${Current}${File2}if [ -f ${FilePath2} ]; then    rm ${FilePath2}fised -e 's/begin/tt/g' ${FilePath} > ${FilePath2}

0 0