One-Day Study Shell: Part 1 The first Shell & Shell Variable & String & Array & Comment

本文介绍了Shell脚本的基础知识,包括创建并运行第一个Shell脚本、定义和使用变量、字符串操作、数组的使用以及注释的添加方式。详细讲解了变量的命名规则、赋值方法、只读变量和删除变量,同时探讨了字符串的长度获取、子串提取以及查找字符的方法。此外,还介绍了Shell中的一维数组定义、访问和长度计算。通过实例代码展示了如何在实践中应用这些概念。
摘要由CSDN通过智能技术生成

The first Shell

  • Create a new file “hello.sh”, write codes :
#!/bin/bash
echo "Hello Shell !"
  • Run Shell: cd to the directory of the file "hello.sh"

执行Bash脚本文件方法:

chmod +x ./hello.sh
./hello.sh 

第二种:

sh hello.sh
or
bash hello.sh 
  • Output:

在这里插入图片描述

Shell Variable

  • Rule1: Only English,number(cannot be the first), and_
  • Rule2: No space
  • Rule3: No punctuation(,./~…)
  • Rule4:No keywords in bash(bash --help)
Valid variable
KATHY
Kathy_Wang
_kathy
kathy16
Invalid variable
16kathy
?kathy
  • Define variables
# no sapce between variable name and "="
name="kathy"
  • Assign value to variable
#1.direct assign value
name="kathy"
#2.use statement 
for file in $(ls /sys)
  • Use variable : echo $variable
name="kathy"
#Method 1
echo $name
#Method 2
echo ${name}
  • Read-only variable
    use readonly to set the variable to read-only variable, the value of read-only variable can not be changed
#!/bin/bash
name="kathy"
echo ${name}
readonly name
name="keke"

在这里插入图片描述

  • Delete variable unset variable_name
#!/bin/bash
name="kathy"
unset name
echo ${name}

在这里插入图片描述

  • Variable types
    1.Local variable
    2.Environment variable
    3.shell variable

Shell String

  • Apostrophe ''
    Just output in ‘’, cannot use variable
#!/bin/bash
name="kathy"
str='Your name is \"$name\"! \n'
echo $str

在这里插入图片描述

  • Double quotes'' ''
    Can use variable and escape character
#!/bin/bash
name="kathy"
str="Your name is \"$name\"! \n"
echo $str

在这里插入图片描述

  • Get string length echo ${#string_name}
#!/bin/bash
str="name"
echo ${#str}

在这里插入图片描述

  • Extract substring echo ${string_name:from:to}
#!/bin/bash
str="What is your name?"
echo ${str:2:6}

在这里插入图片描述

  • Find string `echo expr index "$string_name" wanted_char
#!/bin/bash
str="What is your name?"
echo `expr index "$str" t`

在这里插入图片描述

Shell Array

bash supports just one-dimensional array(no multi-dimensional array), and there is no limit to the size of the array.

  • Define array array_name=(value0 value1 value2)
# Method1
array_name=(
value0
value1
value2
)

#Method2
array_name[0]=value0
array_name[1]=value1
array_name[2]=value2
  • Read array ${array_name[index]}
    Get all elements in array by echo ${array_name[@]}
#!/bin/bash
array_name=(name age school)
echo ${array_name[1]}

在这里插入图片描述

#!/bin/bash
array_name=(name age school)
echo ${array_name[@]}

在这里插入图片描述

  • Get array length ${#array_name[@]}
#!/bin/bash
array_name=(name age school)
_1st_method_array_length=${#array_name[@]}
_2nd_method_array_length=${#array_name[*]}
single_element_length=${#array_name[2]}
echo ${_1st_method_array_length}
echo ${_2nd_method_array_length}
echo ${single_element_length}

在这里插入图片描述

Shell Comment

  • Add #to each line
#-------------------------
#Name
#author:
#site:
#-------------------------
##### start #####
#
#
# some comments
##### end  #####
  • Multi-line comment
:<<EOF
Comment content...
Comment content...
Comment content...
EOF
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kxwang_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值