Ai
3 Star 5 Fork 2

Aron/ShellLearning

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
syntax.sh 4.05 KB
一键复制 编辑 原始数据 按行查看 历史
Aron 提交于 2020-03-02 18:40 +08:00 . update
#!/bin/bash
### 变量
echo "=======变量======="
str="string"
echo $str
echo ${str}
str=123
echo $str
str2=$str
echo $str2;
str3=${str}
echo ${str3}
curDir=$(pwd)
echo "curDir = ${curDir}"
curDirCon=`ls`
echo "curDirCon = ${curDirCon}"
### 打印
echo "=======打印======="
str4="string4"
echo $str4
echo "str4=$str4"
echo "str4=${str4}str3=${str3}"
### 运算
echo "=======expr运算======="
result=$(expr 5 + 5)
echo ${result}
result=$(expr 16 - 5)
echo ${result}
result=$(expr 5 \* 5)
echo ${result}
result=$(expr 28 / 5)
echo ${result}
echo "=======[]运算======="
result=$[5 + 5]
echo ${result}
result=$[16 - 5]
echo ${result}
result=$[5 * 5]
echo ${result}
result=$[28 / 5]
echo ${result}
### 控制
echo "=======控制======="
if [[ 3 > 7 ]]; then
echo "hehe"
else
echo "yes"
fi
echo "9*9======="
i=1
j=1
line=""
while [[ i -lt 10 ]]; do
j=1
line=""
until [[ j -eq 10 ]]; do
if [[ j -le i ]]; then
result=$(expr $i \* $j)
resultStr="$j X $i = $result"
line=${line}${resultStr}"\t"
fi
j=$(expr $j + 1)
done
echo -e ${line}
i=$(expr $i + 1)
done
echo "=======控制字符串比较======="
str1="abc"
str2="abd"
if [[ $str1 > $str2 ]]; then
echo "$str1 大于 $str2"
else
echo "$str1 小于等于 $str2"
fi
if [[ -z $str1 ]]; then
echo "str1 为空"
else
echo "str1 不为空"
fi
str1=""
if [[ -z $str1 ]]; then
echo "str1 为空"
else
echo "str1 不为空"
fi
echo "=======控制文件比较======="
file="syntax.sh"
if [[ -e $file ]]; then
echo "${file} 文件存在"
else
echo "${file} 文件不存在"
fi
if [[ -f $file ]]; then
echo "${file} 是一个文件"
else
echo "${file} 不是一个文件"
fi
if [[ -d $file ]]; then
echo "${file} 是一个文件夹"
else
echo "${file} 不是一个文件夹"
fi
echo "=======循环for======="
num=0
for (( i = 0; i < 10; i++ )); do
num=$[$num + $i]
done
echo "result = ${num}"
echo "=======循环for in======="
file="data"
IFS_OLD=$IFS
IFS=$'\n'
for line in $(cat $file)
do
echo "${line}"
done
IFS=${IFS_OLD}
echo "=======方法======="
function func1 {
echo "func1 invoked"
# 最大的返回值为256,超过了256取模的结果,280%256=24,最终返回24
return 280;
}
func2() {
echo "return value"
}
# 检测文件夹存在的方法,结果保存在全局变量`CheckInputDestDirRecursiveReturnValue`中
# 参数一:检测的文件夹路径
# 参数二:提示消息字符串
# 使用方式如下,去掉注释
# # 导入工具脚本
# . ./FileUtil.sh
# # 检测class_search_dir
# checkDirCore $class_search_dir "指定类的查找目录不存在"
# class_search_dir=${CheckInputDestDirRecursiveReturnValue}
checkDirCore() {
to_process_dir=$1
message=$2
echo "scriptName=${0} paramsCount=${#}"
# 需处理源码目录检查
if [[ -d $to_process_dir ]]; then
echo "目录存在 $to_process_dir"
CheckInputDestDirRecursiveReturnValue=$to_process_dir
return 1
else
echo "${message} ${to_process_dir}"
checkInputDestDirRecursive ${to_process_dir}
fi
}
echo `func1`
echo `func2`
func1
retValue=$?
echo "func1 retValue=$retValue"
retValue=`func2`
echo "func2 retValue=$retValue"
checkDirCore $(pwd) "指定类的查找目录不存在"
dir=${CheckInputDestDirRecursiveReturnValue}
echo "dir = ${dir}"
## 文件
echo "=======文件======="
file="data"
IFS_OLD=$IFS
IFS=$'\n'
for line in $(cat $file)
do
echo "${line}"
done
IFS=${IFS_OLD}
echo "=======文件目录======="
function read_implement_file_recursively {
if [[ -d $1 ]]; then
for item in $(ls $1); do
itemPath="$1/${item}"
if [[ -d $itemPath ]]; then
# 目录
echo "处理目录 ${itemPath}"
read_implement_file_recursively $itemPath
else
# 文件
echo "处理文件 ${itemPath}"
fi
done
else
echo "err:不是一个目录"
fi
}
read_implement_file_recursively $(pwd)
file="subfolder/data2"
destfile="subfolder/data2-p"
sort ${file} | uniq > ${destfile}
## 模块
echo "=======模块======="
# . ./Math.sh
source ./Math.sh
result=$(power 3 5)
echo "3^5 = ${result}"
for i in {1..9}; do
for j in $(seq $i); do
echo -ne "$j x $i = $(($i * $j))\t"
done; echo
done;
echo "======= ======="
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Shell
1
https://gitee.com/dhar/ShellLearning.git
git@gitee.com:dhar/ShellLearning.git
dhar
ShellLearning
ShellLearning
master

搜索帮助