Shell Scripting Exercises

Source: Internet
Author: User

#!/bin/bash
echo "Current datetime is:" ' Date '
echo "Hello world!"

#使用变量: You can use single or double quotation marks
Your_name= "Wang"
Echo $your _name
Echo ${your_name}
Str= ' Shellstudy '
Echo ${str}
#只读变量: Cannot change its value
Myurl= "Www.wnn1.com"
ReadOnly Myurl
echo "My URL is ${myurl}"
Myurl= "Www.wnn.com"
#删除变量: Read-only variables cannot be deleted
Unset Myurl
echo "My URL is ${myurl}"
#获取字符串长度
echo "str string is shellstudy and the string length is:" ${#str}
#提取子字符串: The following instance intercepts 4 characters starting from the 2nd character of the string
String= "Wangniannian"
echo "Intercept string" ${string:1:4}
#查找子字符串
echo ' expr index ' $string ' W '

#for循环
For skill in Ada coffe Action java;do
echo "I am good at ${skill}script"
Done


#数组
Array_name= (Wang Love Zhu)
#读取数组
echo "The second element of the array is:" ${array_name[1]}
#使用 @ can get all the elements in the array
echo "All elements of the array are:" ${array_name[@]}
#取得数组元素的个数
length=${#array_name [@]}
echo "Array with the number of elements:" ${length}
#取得数组单个元素的长度
lengthn=${#array_name [2]}
The length of the third element of the echo "array is:" ${lengthn}


#Shell Pass Parameters
echo "Executed file name: $"
echo "First parameter: $ $"
echo "The second parameter is: $"

#Shell operator: Native bash does not support simple math operations, but can be implemented with other commands, such as awk and expr,expr, which are most commonly used.
#expr is an expression calculation tool that uses it to perform the evaluation of an expression.


#算数运算符
Val= ' Expr 2 + 2 '
echo "2+2= $val"

a=10
echo "A's value is ${a}"
B=20
echo "B's value is ${b}"

Val_jia= ' expr $a + $b '
echo "a+b= $val _jia"

val_jian= ' expr $a + $b '
echo "a-b= $val _jian"

# * need to have \ need to escape in front
val_cheng= ' expr $a \* $b '
echo "a*b= $val _cheng"

val_chu= ' expr $b/$a '
echo "B/a= $val _chu"

val_yu= ' expr $b% $a '
echo "B%a= $val _yu"

if [$a = = $b]
Then echo "a equals B"
Fi
if [$a! = $b]
Then echo "A is not equal to B"
Fi

#关系运算符
#-eq detects whether two numbers are equal and returns true for equality
If [$a-eq $b]
Then echo "$a-eq $b: a equals B"
else echo "$a-eq $b: A is not equal to B"
Fi
#ne detects whether two numbers are equal, not equal returns True
If [$a-ne $b]
Then echo "$a-ne $b: A is not equal to B"
else echo "$a-ne $b: a equals B"
Fi
#gt detects if the number on the left is greater than the right, and if yes, returns true
If [$a-gt $b]
Then echo "$a-gt $b: a greater than B"
else echo "$a-gt $b: A is not much more than B"
Fi
#lt detects if the number on the left is less than the right, and if yes, returns true
If [$a-lt $b]
Then echo "$a-lt $b: a less than B"
else echo "$a-lt $b: A is not less than B"
Fi
#ge detects if the number on the left is greater than or equal to the right, and if yes, returns true
If [$a-ge $b]
Then echo "$a-ge $b: A is greater than or equal to B"
else echo "$a-ge $b: a less than B"
Fi
#le detects if the left number is less than or equal to the right, and if yes, returns true
If [$a-le $b]
Then echo "$a-le $b: A is less than or equal to B"
else echo "$a-le $b: a greater than B"
Fi

#布尔运算符

#逻辑运算符

#字符串运算符

#文件测试运算符

#Shell Input and output redirection
#/dev/null file: If you want to execute a command but do not want the output to be displayed on the screen, you can redirect the output to/dev/null:
#/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.

#输出重定向
Who > Users
#追加用 >>
echo "Rookie tutorial: Www.runoob.com" >>users

#输入重定向
Wc-l Users

#Shell函数
Demofun () {
echo "This is my first Shell function!"
}
echo "---function starts execution---"
Demofun
echo "---function completed---"

#Shell Process Control

If [$ (ps-ef | grep-c "SSH")-GT 1]
Then echo "true"
else echo "false"
Fi

#grep命令
#查找包含wang文本的所有行并打印在屏幕上
grep "Wang"/users/wangniannian/test.sh
#不区分大小写的查找wang文本的所有行并打印在屏幕上
Grep-i "Wang"/users/wangniannian/test.sh
#查找包含wang文本的所有行并将匹配行的总数打印在屏幕上
Grep-c "Wang"/users/wangniannian/test.sh
#查找包含wang文本的所有行并打印在屏幕上同时显示行号
Grep-n "Wang"/users/wangniannian/test.sh
#查找不包含wang文本的所有行并打印仔屏幕上
Grep-v "Wang"/users/wangniannian/test.sh


#awk
#用法一: awk ' {[Pattern] action} ' {filenames} # line match statement awk ' can only use single quotes
#每行按空格或TAB分割, 1, 4 items in the output text
awk ' {print $1,$4} ' Log.txt
#格式化输出
awk ' {printf '%-8s%-10s\n ", $1,$4} ' Log.txt

#用法二: Awk-f #-f equivalent to built-in variable FS, specifying split characters, for example, splitting
Awk-f, ' {print $1,$2} ' Log.txt
#或者使用内建变量
awk ' BEGIN {fs= ', '} {print $1,$2} ' Log.txt
#使用多个分隔符, use a space partition first, then use the split result again, split
Awk-f ' [,] ' {print $1,$2,$5} ' Log.txt

#用法三: Awk-v Setting variables
Awk-va = 1 ' {print $1,$1+a} ' Log.txt

Awk-va = 1-vb = S ' {print $1,$1+a,$1b} ' Log.txt

#用法四: awk-f {awk script} {file name}
Awk-f Cal.awk Log.txt

#运算符
#过滤第一列大于2的行
awk ' $1>2 ' Log.txt
#过滤第一列等于2的行
awk ' $1==2 {print $1,$3} ' Log.txt
#过滤第一列大于2并且第二列等于 ' is ' line
awk ' $1>2 && $2== ' is ' {print $1,$2,$3} ' Log.txt


#xargs


#read


#find
Find ~-name adb

Find. -name ADB

Shell Scripting Exercises

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.