mirror of
https://github.com/dunwu/linux-tutorial.git
synced 2024-04-15 19:55:24 +08:00
update scripts
This commit is contained in:
parent
1072e77e75
commit
7d1ce36b6e
@ -23,6 +23,7 @@ insert_final_newline = true
|
|||||||
end_of_line = crlf
|
end_of_line = crlf
|
||||||
|
|
||||||
[*.{java, sh}]
|
[*.{java, sh}]
|
||||||
|
indent_style = tab
|
||||||
indent_size = 4
|
indent_size = 4
|
||||||
|
|
||||||
[*.md]
|
[*.md]
|
||||||
|
@ -20,9 +20,9 @@ echo ${str3}_${str4}
|
|||||||
|
|
||||||
################### 获取字符串长度 ###################
|
################### 获取字符串长度 ###################
|
||||||
text="12345"
|
text="12345"
|
||||||
echo ${#text}
|
echo "${text} length is: ${#text}"
|
||||||
# Output:
|
# Output:
|
||||||
# 5
|
# 12345 length is: 5
|
||||||
|
|
||||||
################### 获取字符串长度 ###################
|
################### 获取字符串长度 ###################
|
||||||
text="12345"
|
text="12345"
|
||||||
@ -37,13 +37,14 @@ echo `expr index "${text}" ll`
|
|||||||
# 3
|
# 3
|
||||||
|
|
||||||
################### 截取关键字左边内容 ###################
|
################### 截取关键字左边内容 ###################
|
||||||
str="feature/1.0.0"
|
full_branch="feature/1.0.0"
|
||||||
branch=`echo ${str#feature/}`
|
branch=`echo ${full_branch#feature/}`
|
||||||
echo "branch is ${branch}"
|
echo "branch is ${branch}"
|
||||||
|
|
||||||
################### 截取关键字右边内容 ###################
|
################### 截取关键字右边内容 ###################
|
||||||
key=`echo ${str%/1.0.0}`
|
full_version="0.0.1-SNAPSHOT"
|
||||||
echo "key is ${key}"
|
version=`echo ${full_version%-SNAPSHOT}`
|
||||||
|
echo "version is ${version}"
|
||||||
|
|
||||||
################### 判断字符串中是否包含子字符串 ###################
|
################### 判断字符串中是否包含子字符串 ###################
|
||||||
result=$(echo "${str}" | grep "feature/")
|
result=$(echo "${str}" | grep "feature/")
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
################### 声明变量 ###################
|
################### 声明变量 ###################
|
||||||
name="world"
|
name="world"
|
||||||
echo "hello ${name}"
|
echo "hello ${name}"
|
||||||
# Output: hello
|
# Output: hello world
|
||||||
|
|
||||||
################### 只读变量 ###################
|
################### 只读变量 ###################
|
||||||
rword="hello"
|
rword="hello"
|
||||||
|
@ -1,22 +0,0 @@
|
|||||||
#!/usr/bin/env bash
|
|
||||||
|
|
||||||
################### 声明变量 ###################
|
|
||||||
name="world"
|
|
||||||
echo "hello ${name}"
|
|
||||||
# Output: hello world
|
|
||||||
|
|
||||||
################### 只读变量 ###################
|
|
||||||
readonly_var="hello"
|
|
||||||
echo ${readonly_var}
|
|
||||||
# Output: hello
|
|
||||||
readonly readonly_var
|
|
||||||
# rword="bye" # 如果放开注释,执行时会报错
|
|
||||||
|
|
||||||
################### 删除变量 ###################
|
|
||||||
dword="hello" # 声明变量
|
|
||||||
echo ${dword} # 输出变量值
|
|
||||||
# Output: hello
|
|
||||||
|
|
||||||
unset dword # 删除变量
|
|
||||||
echo ${dword}
|
|
||||||
# Output: (空)
|
|
@ -1,4 +0,0 @@
|
|||||||
#!/usr/bin/env bash
|
|
||||||
|
|
||||||
folder=$(pwd)
|
|
||||||
echo "current path: ${folder}"
|
|
@ -1,5 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
echo "User info fro userId:$USER"
|
|
||||||
echo UID:$UID
|
|
||||||
echo HOME:$HOME
|
|
@ -1,8 +0,0 @@
|
|||||||
#!/usr/bin/env bash
|
|
||||||
|
|
||||||
days=10
|
|
||||||
guest="Katie"
|
|
||||||
echo "$guest logged in $days days age"
|
|
||||||
guest="Katie2"
|
|
||||||
days=5
|
|
||||||
echo "$guest logged in $days days age"
|
|
56
codes/shell/示例脚本/基本脚本/变量使用示例.sh
Normal file
56
codes/shell/示例脚本/基本脚本/变量使用示例.sh
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
################### 声明变量 ###################
|
||||||
|
name="world"
|
||||||
|
echo "hello ${name}"
|
||||||
|
# Output: hello world
|
||||||
|
|
||||||
|
################### 输出变量 ###################
|
||||||
|
folder=$(pwd)
|
||||||
|
echo "current path: ${folder}"
|
||||||
|
|
||||||
|
################### 只读变量 ###################
|
||||||
|
rword="hello"
|
||||||
|
echo ${rword}
|
||||||
|
# Output: hello
|
||||||
|
readonly rword
|
||||||
|
# rword="bye" # 如果放开注释,执行时会报错
|
||||||
|
|
||||||
|
################### 删除变量 ###################
|
||||||
|
dword="hello" # 声明变量
|
||||||
|
echo ${dword} # 输出变量值
|
||||||
|
# Output: hello
|
||||||
|
|
||||||
|
unset dword # 删除变量
|
||||||
|
echo ${dword}
|
||||||
|
# Output: (空)
|
||||||
|
|
||||||
|
################### 系统变量 ###################
|
||||||
|
echo "UID:$UID"
|
||||||
|
echo LOGNAME:$LOGNAME
|
||||||
|
echo User:$USER
|
||||||
|
echo HOME:$HOME
|
||||||
|
echo PATH:$PATH
|
||||||
|
echo HOSTNAME:$HOSTNAME
|
||||||
|
echo SHELL:$SHELL
|
||||||
|
echo LANG:$LANG
|
||||||
|
|
||||||
|
################### 自定义变量 ###################
|
||||||
|
days=10
|
||||||
|
user="admin"
|
||||||
|
echo "$user logged in $days days age"
|
||||||
|
days=5
|
||||||
|
user="root"
|
||||||
|
echo "$user logged in $days days age"
|
||||||
|
# Output:
|
||||||
|
# admin logged in 10 days age
|
||||||
|
# root logged in 5 days age
|
||||||
|
|
||||||
|
################### 从变量读取列表 ###################
|
||||||
|
colors="Red Yellow Blue"
|
||||||
|
colors=$colors" White Black"
|
||||||
|
|
||||||
|
for color in $colors
|
||||||
|
do
|
||||||
|
echo " $color"
|
||||||
|
done
|
107
codes/shell/示例脚本/基本脚本/字符串使用示例.sh
Normal file
107
codes/shell/示例脚本/基本脚本/字符串使用示例.sh
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
################### 使用单引号拼接字符串 ###################
|
||||||
|
name1='white'
|
||||||
|
str1='hello, '${name1}''
|
||||||
|
str2='hello, ${name1}'
|
||||||
|
echo ${str1}_${str2}
|
||||||
|
# Output:
|
||||||
|
# hello, white_hello, ${name1}
|
||||||
|
|
||||||
|
################### 使用双引号拼接字符串 ###################
|
||||||
|
name2="black"
|
||||||
|
str3="hello, "${name2}""
|
||||||
|
str4="hello, ${name2}"
|
||||||
|
echo ${str3}_${str4}
|
||||||
|
# Output:
|
||||||
|
# hello, black_hello, black
|
||||||
|
|
||||||
|
################### 获取字符串长度 ###################
|
||||||
|
text="12345"
|
||||||
|
echo "${text} length is: ${#text}"
|
||||||
|
# Output:
|
||||||
|
# 12345 length is: 5
|
||||||
|
|
||||||
|
# 获取子字符串
|
||||||
|
text="12345"
|
||||||
|
echo ${text:2:2}
|
||||||
|
# Output:
|
||||||
|
# 34
|
||||||
|
|
||||||
|
################### 查找子字符串 ###################
|
||||||
|
text="hello"
|
||||||
|
echo `expr index "${text}" ll`
|
||||||
|
# Output:
|
||||||
|
# 3
|
||||||
|
|
||||||
|
################### 判断字符串中是否包含子字符串 ###################
|
||||||
|
result=$(echo "${str}" | grep "feature/")
|
||||||
|
if [[ "$result" != "" ]]; then
|
||||||
|
echo "feature/ 是 ${str} 的子字符串"
|
||||||
|
else
|
||||||
|
echo "feature/ 不是 ${str} 的子字符串"
|
||||||
|
fi
|
||||||
|
|
||||||
|
################### 截取关键字左边内容 ###################
|
||||||
|
full_branch="feature/1.0.0"
|
||||||
|
branch=`echo ${full_branch#feature/}`
|
||||||
|
echo "branch is ${branch}"
|
||||||
|
|
||||||
|
################### 截取关键字右边内容 ###################
|
||||||
|
full_version="0.0.1-SNAPSHOT"
|
||||||
|
version=`echo ${full_version%-SNAPSHOT}`
|
||||||
|
echo "version is ${version}"
|
||||||
|
|
||||||
|
################### 字符串分割成数组 ###################
|
||||||
|
str="0.0.0.1"
|
||||||
|
OLD_IFS="$IFS"
|
||||||
|
IFS="."
|
||||||
|
array=( ${str} )
|
||||||
|
IFS="$OLD_IFS"
|
||||||
|
size=${#array[*]}
|
||||||
|
lastIndex=`expr ${size} - 1`
|
||||||
|
echo "数组长度:${size}"
|
||||||
|
echo "最后一个数组元素:${array[${lastIndex}]}"
|
||||||
|
for item in ${array[@]}
|
||||||
|
do
|
||||||
|
echo "$item"
|
||||||
|
done
|
||||||
|
|
||||||
|
################### 判断字符串是否为空 ###################
|
||||||
|
#-n 判断长度是否非零
|
||||||
|
#-z 判断长度是否为零
|
||||||
|
|
||||||
|
str=testing
|
||||||
|
str2=''
|
||||||
|
if [[ -n "$str" ]]
|
||||||
|
then
|
||||||
|
echo "The string $str is not empty"
|
||||||
|
else
|
||||||
|
echo "The string $str is empty"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ -n "$str2" ]]
|
||||||
|
then
|
||||||
|
echo "The string $str2 is not empty"
|
||||||
|
else
|
||||||
|
echo "The string $str2 is empty"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Output:
|
||||||
|
# The string testing is not empty
|
||||||
|
# The string is empty
|
||||||
|
|
||||||
|
################### 字符串比较 ###################
|
||||||
|
str=hello
|
||||||
|
str2=world
|
||||||
|
if [[ $str = "hello" ]]; then
|
||||||
|
echo "str equals hello"
|
||||||
|
else
|
||||||
|
echo "str not equals hello"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ $str2 = "hello" ]]; then
|
||||||
|
echo "str2 equals hello"
|
||||||
|
else
|
||||||
|
echo "str2 not equals hello"
|
||||||
|
fi
|
@ -1,14 +1,14 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
# 创建数组
|
################### 创建数组 ###################
|
||||||
nums=( [ 2 ] = 2 [ 0 ] = 0 [ 1 ] = 1 )
|
nums=( [ 2 ] = 2 [ 0 ] = 0 [ 1 ] = 1 )
|
||||||
colors=( red yellow "dark blue" )
|
colors=( red yellow "dark blue" )
|
||||||
|
|
||||||
# 访问数组的单个元素
|
################### 访问数组的单个元素 ###################
|
||||||
echo ${nums[1]}
|
echo ${nums[1]}
|
||||||
# Output: 1
|
# Output: 1
|
||||||
|
|
||||||
# 访问数组的所有元素
|
################### 访问数组的所有元素 ###################
|
||||||
echo ${colors[*]}
|
echo ${colors[*]}
|
||||||
# Output: red yellow dark blue
|
# Output: red yellow dark blue
|
||||||
|
|
||||||
@ -32,23 +32,23 @@ printf "+ %s\n" "${colors[@]}"
|
|||||||
# + yellow
|
# + yellow
|
||||||
# + dark blue
|
# + dark blue
|
||||||
|
|
||||||
# 访问数组的部分元素
|
################### 访问数组的部分元素 ###################
|
||||||
echo ${nums[@]:0:2}
|
echo ${nums[@]:0:2}
|
||||||
# Output:
|
# Output:
|
||||||
# 0 1
|
# 0 1
|
||||||
|
|
||||||
# 访问数组长度
|
################### 获取数组长度 ###################
|
||||||
echo ${#nums[*]}
|
echo ${#nums[*]}
|
||||||
# Output:
|
# Output:
|
||||||
# 3
|
# 3
|
||||||
|
|
||||||
# 向数组中添加元素
|
################### 向数组中添加元素 ###################
|
||||||
colors=( white "${colors[@]}" green black )
|
colors=( white "${colors[@]}" green black )
|
||||||
echo ${colors[@]}
|
echo ${colors[@]}
|
||||||
# Output:
|
# Output:
|
||||||
# white red yellow dark blue green black
|
# white red yellow dark blue green black
|
||||||
|
|
||||||
# 从数组中删除元素
|
################### 从数组中删除元素 ###################
|
||||||
unset nums[ 0 ]
|
unset nums[ 0 ]
|
||||||
echo ${nums[@]}
|
echo ${nums[@]}
|
||||||
# Output:
|
# Output:
|
@ -1,19 +0,0 @@
|
|||||||
#!/usr/bin/env bash
|
|
||||||
|
|
||||||
# ----------------------------------------------------------------------------------
|
|
||||||
# 根据特定字符将一个字符串分割成数组
|
|
||||||
# ----------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
str="0.0.0.1"
|
|
||||||
OLD_IFS="$IFS"
|
|
||||||
IFS="."
|
|
||||||
array=( ${str} )
|
|
||||||
IFS="$OLD_IFS"
|
|
||||||
size=${#array[*]}
|
|
||||||
lastIndex=`expr ${size} - 1`
|
|
||||||
echo "数组长度:${size}"
|
|
||||||
echo "最后一个数组元素:${array[${lastIndex}]}"
|
|
||||||
for item in ${array[@]}
|
|
||||||
do
|
|
||||||
echo "$item"
|
|
||||||
done
|
|
@ -1,55 +0,0 @@
|
|||||||
#!/usr/bin/env bash
|
|
||||||
|
|
||||||
################### 单引号和双引号 ###################
|
|
||||||
################### 拼接字符串 ###################
|
|
||||||
# 使用单引号拼接
|
|
||||||
name1='white'
|
|
||||||
str1='hello, '${name1}''
|
|
||||||
str2='hello, ${name1}'
|
|
||||||
echo ${str1}_${str2}
|
|
||||||
# Output:
|
|
||||||
# hello, white_hello, ${name1}
|
|
||||||
|
|
||||||
# 使用双引号拼接
|
|
||||||
name2="black"
|
|
||||||
str3="hello, "${name2}""
|
|
||||||
str4="hello, ${name2}"
|
|
||||||
echo ${str3}_${str4}
|
|
||||||
# Output:
|
|
||||||
# hello, black_hello, black
|
|
||||||
|
|
||||||
################### 获取字符串长度 ###################
|
|
||||||
text="12345"
|
|
||||||
echo "${text} length is: ${#text}"
|
|
||||||
# Output:
|
|
||||||
# 12345 length is: 5
|
|
||||||
|
|
||||||
################### 获取字符串长度 ###################
|
|
||||||
text="12345"
|
|
||||||
echo ${text:2:2}
|
|
||||||
# Output:
|
|
||||||
# 34
|
|
||||||
|
|
||||||
################### 查找子字符串 ###################
|
|
||||||
text="hello"
|
|
||||||
echo `expr index "${text}" ll`
|
|
||||||
# Output:
|
|
||||||
# 3
|
|
||||||
|
|
||||||
################### 截取关键字左边内容 ###################
|
|
||||||
full_branch="feature/1.0.0"
|
|
||||||
branch=`echo ${full_branch#feature/}`
|
|
||||||
echo "branch is ${branch}"
|
|
||||||
|
|
||||||
################### 截取关键字右边内容 ###################
|
|
||||||
full_version="0.0.1-SNAPSHOT"
|
|
||||||
version=`echo ${full_version%-SNAPSHOT}`
|
|
||||||
echo "version is ${version}"
|
|
||||||
|
|
||||||
################### 判断字符串中是否包含子字符串 ###################
|
|
||||||
result=$(echo "${str}" | grep "feature/")
|
|
||||||
if [[ "$result" != "" ]]; then
|
|
||||||
echo "feature/ 是 ${str} 的子字符串"
|
|
||||||
else
|
|
||||||
echo "feature/ 不是 ${str} 的子字符串"
|
|
||||||
fi
|
|
52
codes/shell/示例脚本/操作符/字符串比较判断示例.sh
Normal file
52
codes/shell/示例脚本/操作符/字符串比较判断示例.sh
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
x="abc"
|
||||||
|
if [[ -n $1 ]]; then
|
||||||
|
x=$1
|
||||||
|
fi
|
||||||
|
|
||||||
|
y="xyz"
|
||||||
|
if [[ -n $2 ]]; then
|
||||||
|
y=$2
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "x=${x}, y=${y}"
|
||||||
|
|
||||||
|
if [[ ${x} = ${y} ]]; then
|
||||||
|
echo "${x} = ${y} : x 等于 y"
|
||||||
|
else
|
||||||
|
echo "${x} = ${y}: x 不等于 y"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ ${x} != ${y} ]]; then
|
||||||
|
echo "${x} != ${y} : x 不等于 y"
|
||||||
|
else
|
||||||
|
echo "${x} != ${y}: x 等于 y"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ -z ${x} ]]; then
|
||||||
|
echo "-z ${x} : 字符串长度为 0"
|
||||||
|
else
|
||||||
|
echo "-z ${x} : 字符串长度不为 0"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ -n "${x}" ]]; then
|
||||||
|
echo "-n ${x} : 字符串长度不为 0"
|
||||||
|
else
|
||||||
|
echo "-n ${x} : 字符串长度为 0"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ ${x} ]]; then
|
||||||
|
echo "${x} : 字符串不为空"
|
||||||
|
else
|
||||||
|
echo "${x} : 字符串为空"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Execute: ./operator-demo5.sh
|
||||||
|
# Output:
|
||||||
|
# x=abc, y=xyz
|
||||||
|
# abc = xyz: x 不等于 y
|
||||||
|
# abc != xyz : x 不等于 y
|
||||||
|
# -z abc : 字符串长度不为 0
|
||||||
|
# -n abc : 字符串长度不为 0
|
||||||
|
# abc : 字符串不为空
|
49
codes/shell/示例脚本/操作符/文件目录判断使用示例.sh
Normal file
49
codes/shell/示例脚本/操作符/文件目录判断使用示例.sh
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
file="/etc/hosts"
|
||||||
|
|
||||||
|
if [[ -r ${file} ]]; then
|
||||||
|
echo "${file} 文件可读"
|
||||||
|
else
|
||||||
|
echo "${file} 文件不可读"
|
||||||
|
fi
|
||||||
|
if [[ -w ${file} ]]; then
|
||||||
|
echo "${file} 文件可写"
|
||||||
|
else
|
||||||
|
echo "${file} 文件不可写"
|
||||||
|
fi
|
||||||
|
if [[ -x ${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
|
||||||
|
if [[ -s ${file} ]]; then
|
||||||
|
echo "${file} 文件不为空"
|
||||||
|
else
|
||||||
|
echo "${file} 文件为空"
|
||||||
|
fi
|
||||||
|
if [[ -e ${file} ]]; then
|
||||||
|
echo "${file} 文件存在"
|
||||||
|
else
|
||||||
|
echo "${file} 文件不存在"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Execute: ./operator-demo6.sh
|
||||||
|
# Output:(根据文件的实际情况,输出结果可能不同)
|
||||||
|
# /etc/hosts 文件可读
|
||||||
|
# /etc/hosts 文件可写
|
||||||
|
# /etc/hosts 文件不可执行
|
||||||
|
# /etc/hosts 文件为普通文件
|
||||||
|
# /etc/hosts 文件不是个目录
|
||||||
|
# /etc/hosts 文件不为空
|
||||||
|
# /etc/hosts 文件存在
|
59
codes/shell/示例脚本/操作符/比较符使用示例.sh
Normal file
59
codes/shell/示例脚本/操作符/比较符使用示例.sh
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
x=10
|
||||||
|
if [[ -n $1 ]]; then
|
||||||
|
x=$1
|
||||||
|
fi
|
||||||
|
|
||||||
|
y=20
|
||||||
|
if [[ -n $2 ]]; then
|
||||||
|
y=$2
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "x=${x}, y=${y}"
|
||||||
|
|
||||||
|
if [[ ${x} -eq ${y} ]]; then
|
||||||
|
echo "${x} -eq ${y} : x 等于 y"
|
||||||
|
else
|
||||||
|
echo "${x} -eq ${y}: x 不等于 y"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ ${x} -ne ${y} ]]; then
|
||||||
|
echo "${x} -ne ${y}: x 不等于 y"
|
||||||
|
else
|
||||||
|
echo "${x} -ne ${y}: x 等于 y"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ ${x} -gt ${y} ]]; then
|
||||||
|
echo "${x} -gt ${y}: x 大于 y"
|
||||||
|
else
|
||||||
|
echo "${x} -gt ${y}: x 不大于 y"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ ${x} -lt ${y} ]]; then
|
||||||
|
echo "${x} -lt ${y}: x 小于 y"
|
||||||
|
else
|
||||||
|
echo "${x} -lt ${y}: x 不小于 y"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ ${x} -ge ${y} ]]; then
|
||||||
|
echo "${x} -ge ${y}: x 大于或等于 y"
|
||||||
|
else
|
||||||
|
echo "${x} -ge ${y}: x 小于 y"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ ${x} -le ${y} ]]; then
|
||||||
|
echo "${x} -le ${y}: x 小于或等于 y"
|
||||||
|
else
|
||||||
|
echo "${x} -le ${y}: x 大于 y"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Execute: ./operator-demo2.sh
|
||||||
|
# Output:
|
||||||
|
# x=10, y=20
|
||||||
|
# 10 -eq 20: x 不等于 y
|
||||||
|
# 10 -ne 20: x 不等于 y
|
||||||
|
# 10 -gt 20: x 不大于 y
|
||||||
|
# 10 -lt 20: x 小于 y
|
||||||
|
# 10 -ge 20: x 小于 y
|
||||||
|
# 10 -le 20: x 小于或等于 y
|
45
codes/shell/示例脚本/操作符/比较符使用示例2.sh
Normal file
45
codes/shell/示例脚本/操作符/比较符使用示例2.sh
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
x=10
|
||||||
|
if [[ -n $1 ]]; then
|
||||||
|
x=$1
|
||||||
|
fi
|
||||||
|
|
||||||
|
y=20
|
||||||
|
if [[ -n $2 ]]; then
|
||||||
|
y=$2
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "x=${x}, y=${y}"
|
||||||
|
|
||||||
|
if [[ ${x} != ${y} ]]; then
|
||||||
|
echo "${x} != ${y} : x 不等于 y"
|
||||||
|
else
|
||||||
|
echo "${x} != ${y}: x 等于 y"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ ${x} -lt 100 && ${y} -gt 15 ]]; then
|
||||||
|
echo "${x} 小于 100 且 ${y} 大于 15 : 返回 true"
|
||||||
|
else
|
||||||
|
echo "${x} 小于 100 且 ${y} 大于 15 : 返回 false"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ ${x} -lt 100 || ${y} -gt 100 ]]; then
|
||||||
|
echo "${x} 小于 100 或 ${y} 大于 100 : 返回 true"
|
||||||
|
else
|
||||||
|
echo "${x} 小于 100 或 ${y} 大于 100 : 返回 false"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ ${x} -lt 5 || ${y} -gt 100 ]]; then
|
||||||
|
echo "${x} 小于 5 或 ${y} 大于 100 : 返回 true"
|
||||||
|
else
|
||||||
|
echo "${x} 小于 5 或 ${y} 大于 100 : 返回 false"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Execute: ./operator-demo3.sh
|
||||||
|
# Output:
|
||||||
|
# x=10, y=20
|
||||||
|
# 10 != 20 : x 不等于 y
|
||||||
|
# 10 小于 100 且 20 大于 15 : 返回 true
|
||||||
|
# 10 小于 100 或 20 大于 100 : 返回 true
|
||||||
|
# 10 小于 5 或 20 大于 100 : 返回 false
|
33
codes/shell/示例脚本/操作符/比较符使用示例3.sh
Normal file
33
codes/shell/示例脚本/操作符/比较符使用示例3.sh
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
x=10
|
||||||
|
if [[ -n $1 ]]; then
|
||||||
|
x=$1
|
||||||
|
fi
|
||||||
|
|
||||||
|
y=20
|
||||||
|
if [[ -n $2 ]]; then
|
||||||
|
y=$2
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "x=${x}, y=${y}"
|
||||||
|
|
||||||
|
if [[ ${x} -lt 100 && ${y} -gt 100 ]]
|
||||||
|
then
|
||||||
|
echo "${x} -lt 100 && ${y} -gt 100 返回 true"
|
||||||
|
else
|
||||||
|
echo "${x} -lt 100 && ${y} -gt 100 返回 false"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ ${x} -lt 100 || ${y} -gt 100 ]]
|
||||||
|
then
|
||||||
|
echo "${x} -lt 100 || ${y} -gt 100 返回 true"
|
||||||
|
else
|
||||||
|
echo "${x} -lt 100 || ${y} -gt 100 返回 false"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Execute: ./operator-demo4.sh
|
||||||
|
# Output:
|
||||||
|
# x=10, y=20
|
||||||
|
# 10 -lt 100 && 20 -gt 100 返回 false
|
||||||
|
# 10 -lt 100 || 20 -gt 100 返回 true
|
55
codes/shell/示例脚本/操作符/计算符使用示例.sh
Normal file
55
codes/shell/示例脚本/操作符/计算符使用示例.sh
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
x=10
|
||||||
|
if [[ -n $1 ]]; then
|
||||||
|
x=$1
|
||||||
|
fi
|
||||||
|
|
||||||
|
y=20
|
||||||
|
if [[ -n $2 ]]; then
|
||||||
|
y=$2
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "x=${x}, y=${y}"
|
||||||
|
|
||||||
|
val=`expr ${x} + ${y}`
|
||||||
|
echo "${x} + ${y} = $val"
|
||||||
|
|
||||||
|
val=`expr ${x} - ${y}`
|
||||||
|
echo "${x} - ${y} = $val"
|
||||||
|
|
||||||
|
val=`expr ${x} \* ${y}`
|
||||||
|
echo "${x} * ${y} = $val"
|
||||||
|
|
||||||
|
val=`expr ${y} / ${x}`
|
||||||
|
echo "${y} / ${x} = $val"
|
||||||
|
|
||||||
|
val=`expr ${y} % ${x}`
|
||||||
|
echo "${y} % ${x} = $val"
|
||||||
|
|
||||||
|
if [[ ${x} == ${y} ]]; then
|
||||||
|
echo "${x} = ${y}"
|
||||||
|
fi
|
||||||
|
if [[ ${x} != ${y} ]]; then
|
||||||
|
echo "${x} != ${y}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Execute: ./operator-demo.sh
|
||||||
|
# Output:
|
||||||
|
# x=10, y=20
|
||||||
|
# 10 + 20 = 30
|
||||||
|
# 10 - 20 = -10
|
||||||
|
# 10 * 20 = 200
|
||||||
|
# 20 / 10 = 2
|
||||||
|
# 20 % 10 = 0
|
||||||
|
# 10 != 20
|
||||||
|
|
||||||
|
# Execute: ./operator-demo.sh 10 30
|
||||||
|
# Output:
|
||||||
|
# x=10, y=30
|
||||||
|
# 10 + 30 = 40
|
||||||
|
# 10 - 30 = -20
|
||||||
|
# 10 * 30 = 300
|
||||||
|
# 30 / 10 = 3
|
||||||
|
# 30 % 10 = 0
|
||||||
|
# 10 不等于 30
|
29
codes/shell/示例脚本/系统管理/控制远程服务器执行指令.sh
Normal file
29
codes/shell/示例脚本/系统管理/控制远程服务器执行指令.sh
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
#!/bin/bashost="127.0.0.2"
|
||||||
|
user="root"
|
||||||
|
password="root"
|
||||||
|
|
||||||
|
/usr/bin/expect << EOF
|
||||||
|
set timeout 5
|
||||||
|
spawn ssh -o "StrictHostKeyChecking no" ${user}@${host}
|
||||||
|
expect {
|
||||||
|
"yes/no)?" { send "yes\r"; exp_continue }
|
||||||
|
"password:" { send "${password}\r" }
|
||||||
|
}
|
||||||
|
|
||||||
|
expect "root*"
|
||||||
|
send "ssh-keygen -t rsa\r"
|
||||||
|
expect "Enter file in which to save the key*"
|
||||||
|
send "\r"
|
||||||
|
|
||||||
|
expect {
|
||||||
|
"(y/n)?" { send "n\r"; exp_continue }
|
||||||
|
"Enter passphrase*" { send "\r"; exp_continue }
|
||||||
|
"Enter same passphrase again:" { send "\r" }
|
||||||
|
}
|
||||||
|
|
||||||
|
expect "root*"
|
||||||
|
send "df -h\r"
|
||||||
|
expect "root*"
|
||||||
|
send "exit\r"
|
||||||
|
|
||||||
|
EOF
|
@ -10,11 +10,8 @@ function multem {
|
|||||||
echo $[ $1 * $2 ]
|
echo $[ $1 * $2 ]
|
||||||
}
|
}
|
||||||
|
|
||||||
function divem
|
function divem {
|
||||||
|
if [ $2 -ne 0]; then
|
||||||
{
|
|
||||||
if [ $2 -ne 0]
|
|
||||||
then
|
|
||||||
echo $[ $1 / $2 ]
|
echo $[ $1 / $2 ]
|
||||||
else
|
else
|
||||||
echo -1
|
echo -1
|
||||||
|
13
codes/shell/示例脚本/逻辑控制/break使用示例.sh
Normal file
13
codes/shell/示例脚本/逻辑控制/break使用示例.sh
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
################### 使用break跳出外部循环 ###################
|
||||||
|
# 查找 10 以内第一个能整除 2 和 3 的正整数
|
||||||
|
i=1
|
||||||
|
while [[ ${i} -lt 10 ]]; do
|
||||||
|
if [[ $((i % 3)) -eq 0 ]] && [[ $((i % 2)) -eq 0 ]]; then
|
||||||
|
echo ${i}
|
||||||
|
break;
|
||||||
|
fi
|
||||||
|
i=`expr ${i} + 1`
|
||||||
|
done
|
||||||
|
# Output: 6
|
41
codes/shell/示例脚本/逻辑控制/case使用示例.sh
Normal file
41
codes/shell/示例脚本/逻辑控制/case使用示例.sh
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
echo "input param: " $1 $2 $3
|
||||||
|
|
||||||
|
x=0
|
||||||
|
if [[ -n $1 ]]; then
|
||||||
|
x=$1
|
||||||
|
fi
|
||||||
|
|
||||||
|
oper=""
|
||||||
|
if [[ -n $2 ]]; then
|
||||||
|
oper=$2
|
||||||
|
fi
|
||||||
|
|
||||||
|
y=0
|
||||||
|
if [[ -n $3 ]]; then
|
||||||
|
y=$3
|
||||||
|
fi
|
||||||
|
|
||||||
|
exec
|
||||||
|
case ${oper} in
|
||||||
|
+ | add)
|
||||||
|
val=`expr ${x} + ${y}`
|
||||||
|
echo "${x} + ${y} = ${val}"
|
||||||
|
;;
|
||||||
|
- | sub)
|
||||||
|
val=`expr ${x} - ${y}`
|
||||||
|
echo "${x} - ${y} = ${val}"
|
||||||
|
;;
|
||||||
|
* | mul)
|
||||||
|
val=`expr ${x} \* ${y}`
|
||||||
|
echo "${x} * ${y} = ${val}"
|
||||||
|
;;
|
||||||
|
/ | div)
|
||||||
|
val=`expr ${x} / ${y}`
|
||||||
|
echo "${x} / ${y} = ${val}"
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
echo "Unknown oper!"
|
||||||
|
;;
|
||||||
|
esac
|
30
codes/shell/示例脚本/逻辑控制/continue使用示例.sh
Normal file
30
codes/shell/示例脚本/逻辑控制/continue使用示例.sh
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
# 打印10以内的奇数
|
||||||
|
for (( i = 0; i < 10; i ++ )); do
|
||||||
|
if [[ $((i % 2)) -eq 0 ]]; then
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
echo ${i}
|
||||||
|
done
|
||||||
|
# Output:
|
||||||
|
# 1
|
||||||
|
# 3
|
||||||
|
# 5
|
||||||
|
# 7
|
||||||
|
# 9
|
||||||
|
|
||||||
|
# 多重循环中的 continue 用法
|
||||||
|
for (( a = 1; a <= 5; a ++ ))
|
||||||
|
do
|
||||||
|
echo "Iteration $a:"
|
||||||
|
for (( b = 1; b < 3; b ++ ))
|
||||||
|
do
|
||||||
|
if [[ $a -gt 2 ]] && [[ $a -lt 4 ]]
|
||||||
|
then
|
||||||
|
continue 2
|
||||||
|
fi
|
||||||
|
var3=$[ $a * $b ]
|
||||||
|
echo " The result of $a * $b is $var3"
|
||||||
|
done
|
||||||
|
done
|
@ -1,17 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
#continuing an outer loop
|
|
||||||
|
|
||||||
for (( a = 1; a <= 5; a ++ ))
|
|
||||||
do
|
|
||||||
echo "Iteration $a:"
|
|
||||||
for (( b = 1; b < 3; b ++ ))
|
|
||||||
do
|
|
||||||
if [ $a -gt 2 ] && [ $a -lt 4 ]
|
|
||||||
then
|
|
||||||
continue 2
|
|
||||||
fi
|
|
||||||
var3=$[ $a * $b ]
|
|
||||||
echo " The result of $a * $b is $var3"
|
|
||||||
done
|
|
||||||
done
|
|
68
codes/shell/示例脚本/逻辑控制/for循环使用示例.sh
Normal file
68
codes/shell/示例脚本/逻辑控制/for循环使用示例.sh
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
################### for 语句 ###################
|
||||||
|
echo "print 0 to 9"
|
||||||
|
for (( j = 0; j < 10; j ++ )); do
|
||||||
|
echo ${j}
|
||||||
|
done
|
||||||
|
# Output:
|
||||||
|
# print 0 to 9
|
||||||
|
# 0
|
||||||
|
# 1
|
||||||
|
# 2
|
||||||
|
# 3
|
||||||
|
# 4
|
||||||
|
# 5
|
||||||
|
# 6
|
||||||
|
# 7
|
||||||
|
# 8
|
||||||
|
# 9
|
||||||
|
|
||||||
|
################### for in 语句 ###################
|
||||||
|
echo "print 1 to 5"
|
||||||
|
for i in {1..5}; do
|
||||||
|
echo ${i};
|
||||||
|
done
|
||||||
|
# Output:
|
||||||
|
# print 1 to 5
|
||||||
|
# 1
|
||||||
|
# 2
|
||||||
|
# 3
|
||||||
|
# 4
|
||||||
|
# 5
|
||||||
|
|
||||||
|
################### for in 语句遍历文件 ###################
|
||||||
|
DIR=/home/zp
|
||||||
|
for FILE in ${DIR}/*.sh; do
|
||||||
|
mv "$FILE" "${DIR}/scripts"
|
||||||
|
done
|
||||||
|
# 将 /home/zp 目录下所有 sh 文件拷贝到 /home/zp/scripts
|
||||||
|
|
||||||
|
################### 在 for 语句中使用多个变量 ###################
|
||||||
|
for (( x = 1 , y = 10; x <= y; x ++ , y -- ))
|
||||||
|
do
|
||||||
|
echo "$y - $x = $(($y - $x))"
|
||||||
|
done
|
||||||
|
|
||||||
|
################### 嵌套 for 循环 ###################
|
||||||
|
for (( x = 1; x <= 3; x ++ ))
|
||||||
|
do
|
||||||
|
echo "Starting loop $x:"
|
||||||
|
for (( y = 1; y <= 3; y ++ ))
|
||||||
|
do
|
||||||
|
echo "Inside loog: $y:"
|
||||||
|
done
|
||||||
|
done
|
||||||
|
#Output
|
||||||
|
#Starting loop 1:
|
||||||
|
#Inside loog: 1:
|
||||||
|
#Inside loog: 2:
|
||||||
|
#Inside loog: 3:
|
||||||
|
#Starting loop 2:
|
||||||
|
#Inside loog: 1:
|
||||||
|
#Inside loog: 2:
|
||||||
|
#Inside loog: 3:
|
||||||
|
#Starting loop 3:
|
||||||
|
#Inside loog: 1:
|
||||||
|
#Inside loog: 2:
|
||||||
|
#Inside loog: 3:
|
35
codes/shell/示例脚本/逻辑控制/if-elif-else使用示例.sh
Normal file
35
codes/shell/示例脚本/逻辑控制/if-elif-else使用示例.sh
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
################### if 语句 ###################
|
||||||
|
# 写成一行
|
||||||
|
if [[ 1 -eq 1 ]]; then
|
||||||
|
echo "1 -eq 1 result is: true";
|
||||||
|
fi
|
||||||
|
# Output: 1 -eq 1 result is: true
|
||||||
|
|
||||||
|
# 写成多行
|
||||||
|
if [[ "abc" -eq "abc" ]]
|
||||||
|
then
|
||||||
|
echo ""abc" -eq "abc" result is: true"
|
||||||
|
fi
|
||||||
|
# Output: abc -eq abc result is: true
|
||||||
|
|
||||||
|
################### if else 语句 ###################
|
||||||
|
if [[ 2 -ne 1 ]]; then
|
||||||
|
echo "true"
|
||||||
|
else
|
||||||
|
echo "false"
|
||||||
|
fi
|
||||||
|
# Output: true
|
||||||
|
|
||||||
|
################### if elif else 语句 ###################
|
||||||
|
x=10
|
||||||
|
y=20
|
||||||
|
if [[ ${x} > ${y} ]]; then
|
||||||
|
echo "${x} > ${y}"
|
||||||
|
elif [[ ${x} < ${y} ]]; then
|
||||||
|
echo "${x} < ${y}"
|
||||||
|
else
|
||||||
|
echo "${x} = ${y}"
|
||||||
|
fi
|
||||||
|
# Output: 10 < 20
|
@ -1,11 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
#testing the else section
|
|
||||||
testuser=badtest
|
|
||||||
if grep $testuser /etc/passwd
|
|
||||||
then
|
|
||||||
echo The files for user $testuser are:
|
|
||||||
ls -a /home/.b*
|
|
||||||
else
|
|
||||||
echo "The user name $testuser does not exist on this system"
|
|
||||||
fi
|
|
@ -1,13 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
#testing the if statement
|
|
||||||
if date
|
|
||||||
then
|
|
||||||
echo "it worked"
|
|
||||||
fi
|
|
||||||
echo -e '\n'
|
|
||||||
if asd
|
|
||||||
then
|
|
||||||
echo "it not worked"
|
|
||||||
fi
|
|
||||||
echo 'We are outside the if statement'
|
|
14
codes/shell/示例脚本/逻辑控制/select-demo.sh
Normal file
14
codes/shell/示例脚本/逻辑控制/select-demo.sh
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
PS3="Choose the package manager: "
|
||||||
|
select ITEM in bower npm gem pip
|
||||||
|
do
|
||||||
|
echo -n "Enter the package name: " && read PACKAGE
|
||||||
|
case ${ITEM} in
|
||||||
|
bower) bower install ${PACKAGE} ;;
|
||||||
|
npm) npm install ${PACKAGE} ;;
|
||||||
|
gem) gem install ${PACKAGE} ;;
|
||||||
|
pip) pip install ${PACKAGE} ;;
|
||||||
|
esac
|
||||||
|
break # 避免无限循环
|
||||||
|
done
|
27
codes/shell/示例脚本/逻辑控制/until使用示例.sh
Normal file
27
codes/shell/示例脚本/逻辑控制/until使用示例.sh
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
x=0
|
||||||
|
until [[ ${x} -ge 5 ]]; do
|
||||||
|
echo ${x}
|
||||||
|
x=`expr ${x} + 1`
|
||||||
|
done
|
||||||
|
# Output:
|
||||||
|
# 0
|
||||||
|
# 1
|
||||||
|
# 2
|
||||||
|
# 3
|
||||||
|
# 4
|
||||||
|
|
||||||
|
x=0
|
||||||
|
until echo $x
|
||||||
|
[[ $x -ge 5 ]]
|
||||||
|
do
|
||||||
|
x=$[ $x + 1 ]
|
||||||
|
done
|
||||||
|
# Output:
|
||||||
|
# 0
|
||||||
|
# 1
|
||||||
|
# 2
|
||||||
|
# 3
|
||||||
|
# 4
|
||||||
|
# 5
|
51
codes/shell/示例脚本/逻辑控制/while循环使用示例.sh
Normal file
51
codes/shell/示例脚本/逻辑控制/while循环使用示例.sh
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
################### while 循环输出 0 ~ 9 的平方数 ###################
|
||||||
|
x=0
|
||||||
|
while [[ ${x} -lt 10 ]]; do
|
||||||
|
echo $((x * x))
|
||||||
|
x=$((x + 1))
|
||||||
|
done
|
||||||
|
# Output:
|
||||||
|
# 0
|
||||||
|
# 1
|
||||||
|
# 4
|
||||||
|
# 9
|
||||||
|
# 16
|
||||||
|
# 25
|
||||||
|
# 36
|
||||||
|
# 49
|
||||||
|
# 64
|
||||||
|
# 81
|
||||||
|
|
||||||
|
################### while 循环输出 0 ~ 9 ###################
|
||||||
|
x=0
|
||||||
|
while echo ${x}
|
||||||
|
[[ ${x} -lt 9 ]]
|
||||||
|
do
|
||||||
|
x=$((x + 1))
|
||||||
|
done
|
||||||
|
# Output:
|
||||||
|
# 0
|
||||||
|
# 1
|
||||||
|
# 2
|
||||||
|
# 3
|
||||||
|
# 4
|
||||||
|
# 5
|
||||||
|
# 6
|
||||||
|
# 7
|
||||||
|
# 8
|
||||||
|
# 9
|
||||||
|
|
||||||
|
################### while 循环嵌套 for 循环 ###################
|
||||||
|
x=5
|
||||||
|
while [[ $x -ge 0 ]]
|
||||||
|
do
|
||||||
|
echo "Outer loop: $x"
|
||||||
|
for (( y = 1; $y < 3; y ++ ))
|
||||||
|
do
|
||||||
|
z=$[ $x * $y ]
|
||||||
|
echo "Inner loop: $x * $y = $z"
|
||||||
|
done
|
||||||
|
x=$[ $x - 1 ]
|
||||||
|
done
|
@ -1,16 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
# placing a for loop inside a while loop
|
|
||||||
|
|
||||||
var1=5
|
|
||||||
|
|
||||||
while [ $var1 -ge 0 ]
|
|
||||||
do
|
|
||||||
echo "Outer loop: $var1"
|
|
||||||
for (( var2 = 1; $var2 < 3; var2 ++ ))
|
|
||||||
do
|
|
||||||
var3=$[ $var1 * $var2 ]
|
|
||||||
echo "Inner loop: $var1 * $var2 = $var3"
|
|
||||||
done
|
|
||||||
var1=$[ $var1 - 1 ]
|
|
||||||
done
|
|
@ -1,12 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
#nesting for loops
|
|
||||||
|
|
||||||
for (( a = 1; a <= 3; a ++ ))
|
|
||||||
do
|
|
||||||
echo "Starting loop $a:"
|
|
||||||
for (( b = 1; b <= 3; b ++ ))
|
|
||||||
do
|
|
||||||
echo "Inside loog: $b:"
|
|
||||||
done
|
|
||||||
done
|
|
@ -1,12 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
# using a variable to hold the list
|
|
||||||
|
|
||||||
list="Alabama Alaska Arizona"
|
|
||||||
list=$list" Connecticut"
|
|
||||||
|
|
||||||
for state in $list
|
|
||||||
do
|
|
||||||
echo "Have you ever visited $state"
|
|
||||||
done
|
|
||||||
|
|
@ -1,8 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
#testing the C-style for loop
|
|
||||||
|
|
||||||
for (( i = 1; i <= 10; i ++ ))
|
|
||||||
do
|
|
||||||
echo "The next number is $i"
|
|
||||||
done
|
|
@ -1,17 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
# break n,默认为1
|
|
||||||
|
|
||||||
for (( a = 1; a <= 3; a ++ ))
|
|
||||||
do
|
|
||||||
echo "Outer loop : $a"
|
|
||||||
for (( b = 1; b < 100; b ++ ))
|
|
||||||
do
|
|
||||||
if [ $b -gt 4 ]
|
|
||||||
then
|
|
||||||
break 2
|
|
||||||
fi
|
|
||||||
echo " Inner loop:$b"
|
|
||||||
done
|
|
||||||
done
|
|
||||||
|
|
@ -1,15 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
#using the case command
|
|
||||||
|
|
||||||
case $USER in
|
|
||||||
tiandi | barbar)
|
|
||||||
echo "Welcome, $USER"
|
|
||||||
echo "Pleas enjoy your visit" ;;
|
|
||||||
testing)
|
|
||||||
echo "Special testing account" ;;
|
|
||||||
jessica)
|
|
||||||
echo "Do not forget to logout when you are out" ;;
|
|
||||||
*)
|
|
||||||
echo "Sorry, you are not allowed here" ;;
|
|
||||||
esac
|
|
@ -1,12 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
#using the continue command
|
|
||||||
|
|
||||||
for (( var1 = 1; var1 < 15; var1 ++ ))
|
|
||||||
do
|
|
||||||
if [ $var1 -gt 5 ] && [ $var1 -lt 10 ]
|
|
||||||
then
|
|
||||||
continue
|
|
||||||
fi
|
|
||||||
echo "Iteration number:$var1"
|
|
||||||
done
|
|
@ -1,19 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
# looking for a possible value
|
|
||||||
|
|
||||||
if [ $USER = "tiandi" ]
|
|
||||||
then
|
|
||||||
echo "Welcome $USER"
|
|
||||||
echo "Please enjoy your visit"
|
|
||||||
elif [ $USER = testing ]
|
|
||||||
then
|
|
||||||
echo "Welcome $USER"
|
|
||||||
echo "Please enjoy your visit"
|
|
||||||
elif [ $USER = barbar ]
|
|
||||||
then
|
|
||||||
echo "Do not forget to logout when you're done"
|
|
||||||
else
|
|
||||||
echo "Sorry, you are not allowed here"
|
|
||||||
fi
|
|
||||||
|
|
@ -1,16 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
var1=10
|
|
||||||
var2=5
|
|
||||||
if [ $var1 -gt 5 ]
|
|
||||||
then
|
|
||||||
echo "The test value $var1 is greater than 5"
|
|
||||||
fi
|
|
||||||
if [ $var1 -eq $var2 ]
|
|
||||||
then
|
|
||||||
echo "The values is equal"
|
|
||||||
else
|
|
||||||
echo "The values are different"
|
|
||||||
fi
|
|
||||||
|
|
||||||
|
|
@ -1,21 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
#using the until command
|
|
||||||
|
|
||||||
var1=100
|
|
||||||
until [ $var1 -eq 0 ]
|
|
||||||
do
|
|
||||||
echo $var1
|
|
||||||
var1=$[ $var1 - 25 ]
|
|
||||||
done
|
|
||||||
|
|
||||||
var1=100
|
|
||||||
until echo $var1
|
|
||||||
[ $var1 -eq 0 ]
|
|
||||||
do
|
|
||||||
echo Inside the loop: $var1
|
|
||||||
var1=$[ $var1 - 25 ]
|
|
||||||
done
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,10 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
# while command test
|
|
||||||
var1=10
|
|
||||||
while [ $var1 -gt 0 ]
|
|
||||||
do
|
|
||||||
echo $var1
|
|
||||||
var1=$[ $var1 - 1 ]
|
|
||||||
done
|
|
||||||
|
|
@ -1,11 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
#testing a multicommand while loop
|
|
||||||
|
|
||||||
var1=10
|
|
||||||
while echo $var1
|
|
||||||
[ $var1 -ge 0 ]
|
|
||||||
do
|
|
||||||
echo 'This is inside the loop'
|
|
||||||
var1=$[ $var1 - 1 ]
|
|
||||||
done
|
|
@ -1,8 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
# another example of how not to use the for command
|
|
||||||
|
|
||||||
for test in Newada "New Hampshire"
|
|
||||||
do
|
|
||||||
echo "Now going to $test"
|
|
||||||
done
|
|
@ -7,9 +7,18 @@
|
|||||||
val1=baseball
|
val1=baseball
|
||||||
val2=hockey
|
val2=hockey
|
||||||
|
|
||||||
if [ $val1 > $val2 ]
|
################### 错误使用大于小于号 ##################
|
||||||
|
if [[ $val1 > $val2 ]]
|
||||||
then
|
then
|
||||||
echo "$val1 is greater than $val2"
|
echo "$val1 is greater than $val2"
|
||||||
else
|
else
|
||||||
echo "$val1 is less than $val2"
|
echo "$val1 is less than $val2"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
################### 正确使用大于小于号 ###################
|
||||||
|
if [[ $val1 \> $val2 ]]
|
||||||
|
then
|
||||||
|
echo "$val1 is greater than $val2"
|
||||||
|
else
|
||||||
|
echo "$val1 is less than $val2"
|
||||||
|
fi
|
@ -1,10 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
#testing compound comparisons
|
|
||||||
|
|
||||||
if [ -d $HOME ] && [ -w $HOME/testing ]
|
|
||||||
then
|
|
||||||
echo "The file exists and you can write to it"
|
|
||||||
else
|
|
||||||
echo "I cannot write to it"
|
|
||||||
fi
|
|
@ -1,30 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
# testing string length
|
|
||||||
|
|
||||||
#-n 判断长度是否非零
|
|
||||||
#-z 判断长度是否为零
|
|
||||||
|
|
||||||
val1=testing
|
|
||||||
val2=''
|
|
||||||
|
|
||||||
if [ -n "$val1" ]
|
|
||||||
then
|
|
||||||
echo "The string $val1 is not empty"
|
|
||||||
else
|
|
||||||
echo "The string $val1 is empty"
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ -z "$val2" ]
|
|
||||||
then
|
|
||||||
echo "The string $val2 is empty"
|
|
||||||
else
|
|
||||||
echo "The string $val2 is not empty"
|
|
||||||
fi
|
|
||||||
if [ -z "$val3" ]
|
|
||||||
then
|
|
||||||
echo "The string $val3 is empty"
|
|
||||||
else
|
|
||||||
echo "The string $val3 is not empty"
|
|
||||||
fi
|
|
||||||
|
|
@ -1,8 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
# multiple variables
|
|
||||||
|
|
||||||
for (( a = 1 , b = 10; a <= 10; a ++ , b -- ))
|
|
||||||
do
|
|
||||||
echo "$a - $b"
|
|
||||||
done
|
|
@ -1,9 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
#testing multiple commands in the then section
|
|
||||||
testuser=tiandi
|
|
||||||
if grep $testuser /etc/passwd
|
|
||||||
then
|
|
||||||
echo The bash files from user $testuser are:
|
|
||||||
ls -a /home/$testuser/.b*
|
|
||||||
fi
|
|
@ -1,10 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
#testing string equality
|
|
||||||
|
|
||||||
testuser=tiandi
|
|
||||||
|
|
||||||
if [ $USER = $testuser ]
|
|
||||||
then
|
|
||||||
echo "Welcome $testuser"
|
|
||||||
fi
|
|
@ -1,2 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
@ -1,21 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
# checking if a directory or a file exists
|
|
||||||
|
|
||||||
if [ -e $HOME ]
|
|
||||||
then
|
|
||||||
echo "OK on the directory.now to check the file"
|
|
||||||
#checking if a file exists
|
|
||||||
if [ -e $HOME/testing ]
|
|
||||||
then
|
|
||||||
#the file exists,append data to it
|
|
||||||
echo "Appending date to existing file"
|
|
||||||
date >> $HOME/testing
|
|
||||||
else
|
|
||||||
#the file is not exists,create a new file
|
|
||||||
echo "Creating a new file"
|
|
||||||
date > $HOME/testing
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
echo 'Sorry. you do not have a $HOME directory'
|
|
||||||
fi
|
|
@ -1,12 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
# look before you leap
|
|
||||||
|
|
||||||
if [ -d $HOME ]
|
|
||||||
then
|
|
||||||
echo "Your home directory exists"
|
|
||||||
cd $HOME
|
|
||||||
ls -a
|
|
||||||
else
|
|
||||||
echo "There is a problem with your HOME direcotry"
|
|
||||||
fi
|
|
@ -1,13 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
#正确使用大于小于号
|
|
||||||
|
|
||||||
val1=baseball
|
|
||||||
val2=hocky
|
|
||||||
|
|
||||||
if [ $val1 \> $val2 ]
|
|
||||||
then
|
|
||||||
echo "$val1 is greater than $val2"
|
|
||||||
else
|
|
||||||
echo "$val1 is less than $val2"
|
|
||||||
fi
|
|
@ -1,6 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
for test in I don\'t know if "this'll" work
|
|
||||||
do
|
|
||||||
echo "word:$test"
|
|
||||||
done
|
|
@ -1,7 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
# basic for command
|
|
||||||
for test in Alabama Alaska Arizona
|
|
||||||
do
|
|
||||||
echo The next state is $test
|
|
||||||
done
|
|
Loading…
Reference in New Issue
Block a user