linux bash吧,还有啥Bourne Again Shell

linux bash吧,还有啥Bourne Again Shell

bash吧,还有啥Bourne Again Shell

头部要写
#!/bin/bash

set -x #open script debug

1* 添加执行权限
chmod +x 脚本名
chmod +x testsh1.sh

2*执行脚本
当前路径执行
./脚本名
testsh1.sh

全路径执行
/tmp/vskedtest/testsh1.sh

解释器执行
/bin/sh 脚本路径
/bin/sh testsh1.sh
/bin/sh /tmp/vskedtest/testsh1.sh

3*遍历目录下文件
shpfilepath=`ls /tmp/vskedtest/*.shp` #定义遍历的目录

for shpFile in $shpfilepath
do
echo $shpFile
done

4*执行指定脚本
source 脚本路径

5* for循环数值
for j in $(seq 1 3)
do
echo $j #1 2 3
done

快速生成文件示例
for j in $(seq 1 10)
do 
echo $j>$j.xml
done

6* 变量自增1
1 i=`expr $i + 1`;
2 let i+=1;
3 ((i++));
4 i=$[$i+1];
5 i=$(( $i + 1 ))

7* 接收传入参数
$0 正在执行路径本身
$1 第一个参数
$2 第二个参数

8* 延时
sleep 秒数
sleep 1

9*开启或关闭脚本调试
开启
set -x
关闭
set +x
需要写到#!/bin/bash行下或
/bin/sh -x xxxx.sh

10*执行外部命令
pgsql_path=/usr/bin
$pgsql_path/shp2pgsql -s 3857 -c -W

11* 获取系统当前时间
原始时间
echo $(date)
格式化输出
echo $(date "+%Y-%m-%d %H:%M:%S")


12* 输出
输出到控制台
echo "字符串或变量"
输出到文件覆盖方式
echo "abc" > /opt/testfile111
输出到文件追加方式
echo "abc122" >> /opt/testfile111

13* 定义函数

function myfun1(){
echo "this is myoupt"
echo "input parameter1第一个参数" $1
echo "input parameter2第二个参数" $2

}
函数调用
myfun1 hello world

14* 变量定义与使用

myname="vsked"
echo ${myname}

15* while循环
while condition
do
command
done

有限循环
int=1
while(( $int<=5 ))
do
echo $int
let "int++"
done
无限循环
int=1
while :
do
echo $int
let "int++"
done
示例

interval=6

while : 
do
echo "666"
sleep $interval
echo "here is 666"
sleep $interval
echo "good idea"

done

16* 执行系统命令(注意是反引号!)
sourceFolderom="/opt/testsh/om/"

for omFile in `ls $sourceFolderom`
do
echo $omFile
done

原文地址:https://www.cnblogs.com/xinxihua/p/12563811.html