Custom functions in the shell

Source: Internet
Author: User

Customizing a function for printing function parameters

[[email protected] shell]# vim fun1.sh#!/bin/bashpri(){        echo "第一个参数为 $1"        echo "第二个参数为 $2"        echo "文件名为 $0"        echo "总共有$#个参数"       }pri a b       #调用函数

Execution results

[[email protected] shell]# sh fun1.sh第一个参数为 a第二个参数为 b文件名为 fun1.sh总共有2个参数

Customize a function that will add calculations

[[email protected] shell]# vim fun2.sh#!/bin/bashsum (){      s=$[$1+$2]      echo $s      }sum 6 9

Execution results

[[email protected] shell]# sh -x fun2.sh+ sum 6 9+ s=15+ echo 1515

Customize an input network card name to display IP functions

[[email protected] shell]# vim fun3.sh#!/bin/baship (){      a=`ifconfig |grep -A1 "$1: "`      b=`ifconfig |grep -A1 "$1: "|awk ‘/inet/ {print $2}‘`      if [ -z "$a" ]      then          echo "网卡不存在"          exit      elif [ -z "$b" ]      then          echo "网卡没有定义ip"          exit      else          echo "$b"      fi}read -p "请输入网卡名:" ethip $eth

Execution results

[[email protected] shell]# sh fun3.sh请输入网卡名:ens33192.168.130.116[[email protected] shell]# sh fun3.sh请输入网卡名:ens37网卡没有定义ip[[email protected] shell]# sh fun3.sh请输入网卡名:ens38网卡不存在

Custom functions in the shell

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.