diff --git a/codes/shell/array-demo.sh b/codes/shell/array-demo.sh new file mode 100644 index 0000000..65ff2ef --- /dev/null +++ b/codes/shell/array-demo.sh @@ -0,0 +1,55 @@ +#!/usr/bin/env bash + +# 创建数组 +nums=([2]=2 [0]=0 [1]=1) +colors=(red yellow "dark blue") + +# 访问数组的单个元素 +echo ${nums[1]} +# Output: 1 + +# 访问数组的所有元素 +echo ${colors[*]} +# Output: red yellow dark blue + +echo ${colors[@]} +# Output: red yellow dark blue + +printf "+ %s\n" ${colors[*]} +# Output: +# + red +# + yellow +# + dark +# + blue + +printf "+ %s\n" "${colors[*]}" +# Output: +# + red yellow dark blue + +printf "+ %s\n" "${colors[@]}" +# Output: +# + red +# + yellow +# + dark blue + +# 访问数组的部分元素 +echo ${nums[@]:0:2} +# Output: +# 0 1 + +# 访问数组长度 +echo ${#nums[*]} +# Output: +# 3 + +# 向数组中添加元素 +colors=(white "${colors[@]}" green black) +echo ${colors[@]} +# Output: +# white red yellow dark blue green black + +# 从数组中删除元素 +unset nums[0] +echo ${nums[@]} +# Output: +# 1 2 diff --git a/codes/shell/array/arrayDemo.sh b/codes/shell/array/arrayDemo.sh deleted file mode 100644 index b493c65..0000000 --- a/codes/shell/array/arrayDemo.sh +++ /dev/null @@ -1,71 +0,0 @@ -#!/usr/bin/env bash - -# 声明数组 -## 声明数组方式一 -animals[0]=Cat -animals[1]=Dog -animals[2]=Fish -## 声明数组方式二 -colors=(Red Green Blue) - - -# 根据下标获取数组中的元素 -echo "=========== 根据下标获取数组中的元素 ===========" -echo "\${colors[1]} : " ${colors[1]} -# 输出: -# ${colors[1]} : Green - - -# 数组切片 -echo "=========== 数组切片 ===========" -colors[1]="Dark Green" - -echo "\${colors[*]} : " -printf "+ %s\n" ${colors[*]} -# 输出: -# + Red -# + Dark -# + Green -# + Blue - -echo "\"\${colors[*]}\" : " -printf "+ %s\n" "${colors[*]}" -# 输出: -# + Red Dark Green Blue - -echo "\"\${colors[@]}\" : " -printf "+ %s\n" "${colors[@]}" -# 输出: -# + Red -# + Dark -# + Green -# + Blue - -echo "\"\${colors[@]:0:2}\" : " ${colors[@]:0:2} ### Red Dark Green - -# 向数组中添加元素 -echo "=========== 向数组中添加元素 ===========" -colors=(Yellow "${colors[@]}" Pink Black) -echo "\"\${colors[@]}\" : " ${colors[@]} -# 输出: -# Yellow Red Dark Green Blue Pink Black - -# 向数组中删除元素 -echo "=========== 向数组中删除元素 ===========" -unset colors[0] -echo "\"\${colors[@]}\" : " ${colors[@]} -# 输出: -# Red Dark Green Blue Pink Black - -# 获取数组的长度 -echo "=========== 获取数组的长度 ===========" -## 获取数组的长度方式一 -echo "\${#colors[*]} : ${#colors[*]}" -## 获取数组的长度方式二 -echo "\${#colors[@]} : ${#colors[@]}" - - -for (( i = 0; i < animals; i ++ )); do - echo $i -done - diff --git a/codes/shell/comment-demo.sh b/codes/shell/comment-demo.sh new file mode 100644 index 0000000..0a5e421 --- /dev/null +++ b/codes/shell/comment-demo.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash + +#-------------------------------------------- +# shell 注释示例 +# author:zp +#-------------------------------------------- + +# echo '这是单行注释' + +########## 这是分割线 ########## + +:<