Linux Learning Summary (60) Shell script 4-Functions and arrays

Source: Internet
Author: User

A function in the shell

1 understand several system variables of the script first

$# 是传给脚本的参数个数$0 是脚本本身的名字$1 是传递给该shell脚本的第一个参数$2 是传递给该shell脚本的第二个参数[email protected] 是传给脚本的所有参数的列表$* 是以一个单字符串显示所有向脚本传递的参数,与位置变量不同,参数可超过9个$$ 是脚本运行的当前进程ID号$? 是显示最后命令的退出状态,0表示没有错误,其他表示有错误

2 definition and usage of functions
The function is to organize a piece of code into a small unit, and give the small unit a name, when the code is used to call the name of the small unit directly. Format: function f_name () {command} functions must be placed first, where the keyword function can be omitted
Example 1

vim 6.sh#!/bin/bashinput() {    echo $1 $2 $# $0}input 4 4 4 4 4

The execution result is: 4 4 5 6.sh
The first and second parameter of a shell script passed
$# indicates the number of arguments passed to the script
The name of the script itself
Example 2

#!/bin/bashsum(){s=$[$1+$2]echo $s}sum 1 2

If you want to pass in parameters outside of the script, Sum 1 2 can be changed to sum $ $, and then execute the script with parameters such as SH sum.sh 1 2
Example 3
requirements, write a script, give the network card name, you can display the corresponding IP address
Script debugging
ifconfig |grep ens33 -A1 |grep ‘inet‘ |awk ‘{print $2}‘
The meaning of each paragraph:
Ifconfig Needless to say, you can view information about the NIC
grep enss3-a1 filters out the line containing the NIC name Ens33, and prints its next line in passing. Which is the IP address line we need.
grep ' inet ' to find the line with the IP address, here you can use TAIL-1 instead
awk ' {print $} ' prints the second segment of the IP address line, which is the IP address, and awk does not specify a delimiter, which is separated by a space or tab by default. That is, split with a null character.
Final result

#!/bin/baship(){ifconfig |grep "$1: " -A1 |grep ‘inet‘ |awk ‘{print $2}‘}read -p "please input a name of network card: " nameip $name

Note: The following grep command represents the first argument passed by the function, so be sure to use double quotation marks.
Make a perfect of the above script,
1 Determine if the network card name is the network card in the system, not to give a hint
2 Determine if the NIC is assigned an IP address, no hint

#!/bin/baship(){ifconfig |grep "$1: " -A1 |grep ‘inet‘|awk ‘{print $2}‘}read -p "please input a name of network card: " nameip $namen=`ifconfig |grep "$name"`if [ -z "$n" ];then    echo "the eth is not exist"fim=`ifconfig |grep "$name" -A1 |grep ‘inet‘`if [ -z "$m" ];then    echo "the eth has no ip assigned"fi
Arrays in the two shells

Define array a= (1 2 3 4 5)
echo ${a[@]}//Show entire array, equivalent to echo ${a[*]}
echo ${#a [@]} Gets the number of elements in the array
echo ${a[2]} reads the third element, array element number starting from 0
Array Assignment
a[1]=100; Echo ${a[@]}
a[5]=2; Echo ${a[@]} An element is added automatically if the subscript does not exist
Deletion of arrays
Unset a[1]; echo ${a[*]}//delete 1th element in array
unset A;echo ${a[*]}//Empty array
Array shards
A= (' seq 1 5 ')
Echo ${a[@]:0:3} starts with the first element and intercepts 3
Echo ${a[@]:1:4} starts with the second element and intercepts 4
Echo ${a[@]:0-3:2} Starts from the bottom 3rd element and then intercepts 2
Array substitution
echo ${a[@]/3/100}//replaces 3 in array A with 100 (displays only to the screen and does not change the array)
A= (${a[@]/3/100})//replace 3 in array A with 100 (change array)

Linux Learning Summary (60) Shell script 4-Functions and arrays

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.