shell-code-1

#!/bin/bash

# online test tool: http://www.shucunwang.com/RunCode/shell/

name="pxy"
#Attention for variables' names:
#1. No space between "name" and "="
#2. First letter must be a-z, A-Z
#3. No space or 标点符号punctuation in it

# for file in 'ls /etc'
# list all files' names in /etc. Also assignment

#echo $name
echo ${name}
# when using a variable, add '$' or '${}'(better)
# 第二次赋值不需要在变量前加$

for skill in Ada Coffe Action Java; do
echo "I am good at ${skill}Script"
done
# 加花括号是为了帮助解释器识别变量的边界
# 如果不加,上例中解释器就会把$skillScript当成一个变量(其值为空)
# 加的话就可以正确输出了

readonly name
# then its value cann't be changed

unset name
# delete a variable. cann't delete readonly

#******************************
# 字符串:单引号、双引号、无引号都可以
# 单引号:1) 任何字符都会原样输出;2)其中不能出现单引号(变量、转义符无效)
# 双引号:可以有变量和转义字符
name='pxy '
fullName="you are "$name", right?"
echo $fullName

# 拼接字符串:直接写
name1="I am "${name}
name2=${name}" is me"

# 获取字符串长度
echo ${#name}

# 截取字符串。${字符串名字:start:size}
name="01234567"
echo ${name:2:3}
# output 234

# 查找子串。子串此处为r,输出为1
string="runoob is a great company"
echo `expr index "$string" r`

#******************************
# 数组:
# 定义:1) 数组名=(值1 值2 ... 值n)或者用换行的方式
# 2) 可以单独定义数组的各个分量,可以不使用连续的下标,而且下标的范围没有限制。
arr=(1 2 3)
# get an item. '@' means all items.
echo ${arr[0]}
echo ${arr[@]}
# get length of an item or an array
echo ${#arr[0]}
# for array
echo ${#arr[@]}
echo ${#arr[*]}
#******************************

原文地址:https://www.cnblogs.com/pxy7896/p/6417372.html