Shell Basic Learning

Source: Internet
Author: User

#! /bin/bash

#设置只读变量, read-only variables are not modifiable
Myurl= "Http://www.baidu.com"
ReadOnly Myurl

#删除变量, unset cannot delete read-only variables
myurl1= "Http://www.sina.com"
Unset MYURL1

#运行shell时会同时存在三种变量
#局部变量, environment variables, shell variables

#单引号: cannot have a variable in single quotation marks, cannot have single quotation marks
#双引号: You can have variables in double quotes, and escape characters can appear.
Your_name= "QINJX"
Str= "Hello,i know your name is \" $your _name\ "! \ n "

#拼接字符串
Your_name= "Cye"
Greeting= "Hello," $your _name "!"
Greeting_1= "Hello, $your _name"
echo $greeting $greeting _1

#获取字符串长度
String= "ABCDEFG"
echo ${#string} #输出 7

#提取子字符串, 4 characters are intercepted starting with the second character.
String= "Runoob is a great site"
Echo ${string:1:4} #输出unoo

#查找子字符串, find the position of I or S, "'" in the following script is the inverted quotation mark instead of the single quote "'",
String= "Runoob is a great company"
echo ' expr index ' $string ' is ' #输出8

#数组, defining the array
Array_name= (VALUE0,VALUE1,VALUE2,VALUE3)

#读取数组, ${array name [subscript]}
Valuen=${array_name[n]}

#使用 the @ symbol to get all the elements in the array
Echo ${array_name[@]}

#获取数组的长度
length=${#array_name [@]}
#或者
length=${#array_name [*]}
#取得数组单个元素的长度
lengthn=${#array_name [n]}

#shell传递参数, when executing a shell script to pass parameters to the script, the format of the parameters obtained in the script is: $n, n is a number, 1 represents the first argument to execute the script, and 2 is the second argument that executes the script.
echo "Shell pass argument instance"
echo "file name executed: $ A"
echo "First parameter: $ $"
echo "The second parameter is: $"
echo "The third parameter is: $ $"

#调用时为
#chmod +x test.sh
#./test.sh 1 2 3
#结果为:
#shell Passing Parameter instances
#执行的文件名:./test.sh
#第一个参数为: 1
#第二个参数为: 2
#第三个参数为: 3

#输出输入重定向
Command > file redirects the output to file.
Command < file redirects the input to file.
Command >> file redirects the output to file in an append manner.
n > file redirects the file descriptor n files to filename.
n >> files redirect files with file descriptor N as an append to file.
N >& m merges the output file m and N.
N <& m merges the input file m and N.
"<< Tag" will start to tag the contents of tag and end tag tag as the output

0 is standard input (STDIN), 1 is standard output (STDOUT), 2 is standard error output (STDERR)
/dev/null is a special file, and the content written to it is discarded, and if you try to read from the file, nothing is read. However, the/dev/null file is very useful and redirects the output of the command to it, which results in a "no output" effect.
$ command >/dev/null 2>&1

Shell Basic Learning

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.