Bash Shell中的特殊位置变量及其应用

众所周知bash shell中有许多特殊的位置变量,灵活使用它们可以更好地发挥Shell脚本的功用。

即位置变量:$1,$2,...来表示,用于让脚本在脚本代码中调用通过命令行传递给它的参数

特殊变量:$?, $0,$*,$@,$#, $$

---------------- 具体解析 -----------------------------

$? : 用于检测上一条命令的返回码,0代表成功,1-255表示失败

$0 :  表示命令本身

$*  : 表传递给脚本的所有参数,全部参数合为一个字符串

$@ : 表传递给脚本的所有参数,每个参数为独立字符串

$# :  传递给脚本的参数的个数

$$ :  获取当前执行的Shell脚本的进程号

为了更好理解,我编写如下脚本进行测试,注意$10要括号括起来才能识别。

[root@Franklin13 ~]# echo $SHELL
/bin/bash

[root@Franklin13 ~]# vim test_arg.sh

[root@Franklin13 ~]# bash test_arg.sh {a..z}

1st arg is a
2st arg is b
10st arg is j
All arg is a b c d e f g h i j k l m n o p q r s t u v w x y z
All arg is a b c d e f g h i j k l m n o p q r s t u v w x y z
The arg number is 26
The scriptname is test_arg.sh
[root@Franklin13 ~]#

实战1: 活用$1来编写一个自动取ip的脚本


首先用如下命令来截取ip, 成功了

[root@Franklin13 ~]# ifconfig ens33|grep -w "inet"|tr -s ' ' %|cut -d% -f3 (tr -s ' ' %表示先压缩所有重复的空格到只留一个再转换为%)
192.168.1.19

再通过调用$1来使这条命令可以用于一个脚本来快速查ip。

[root@Franklin13 ~]# ./get-ip.sh lo

The ip is 127.0.0.1
Thank you for using!
[root@Franklin13 ~]# ./get-ip.sh ens33
The ip is 192.168.1.19
Thank you for using!
[root@Franklin13 ~]# ./get-ip.sh virbr0
The ip is 192.168.122.1
Thank you for using!

 --------------------------------全文完-----------------------------------

Bash Shell中的特殊位置变量及其应用的更多相关文章

  1. bash shell中测试命令

    bash shell中测试命令 test命令提供了if-than语句中测试不同条件的途径.如果test命令中列出的条件成立,test命令就会退出并返回退出状态吗0 .这样if-than语句就与其他编程 ...

  2. Bash shell中的位置参数$#,$*,$@,$0,$1,$2...及特殊参数$?,$-等的含义

    http://hi.baidu.com/lolorosa/blog/item/5775a608bd670d33b0351da7.html $# 是传给脚本的参数个数 $@ 是传给脚本的所有参数的列表 ...

  3. Bash Shell中Shift用法分享

    这篇文章主要介绍了Bash Shell中Shift的使用方法,需要的朋友可以参考下 shift可以用来向左移动位置参数.Shell的名字 $0第一个参数 $1第二个参数 $2第n个参数 $n所有参数 ...

  4. linux bash shell中case语句的实例

    本文介绍下,在bash shell编程中,有关case语句的一个例子,学习下case语句的用法,有需要的朋友参考下. 本文转自:http://www.jbxue.com/article/13377.h ...

  5. 【转】Bash Shell中命令行选项/参数处理

    原文网址:http://www.cnblogs.com/FrankTan/archive/2010/03/01/1634516.html 0.引言 写程序的时候经常要处理命令行参数,本文描述在Bash ...

  6. Bash Shell中命令行选项/参数处理

    0.引言 写程序的时候经常要处理命令行参数,本文描述在Bash下的命令行处理方式. 选项与参数: 如下一个命令行: ./test.sh -f config.conf -v --prefix=/home ...

  7. 26 bash shell中的信号

    当没有任何捕获时,一个交互式 Bash Shell 会忽略 SIGTERM(发送到进程的 TERM 信号用于要求进程终止) 和 SIGQUIT(当用户要求进程执行 core dump 时,QUIT 信 ...

  8. My way on Linux - [Shell基础] - Bash Shell中判断文件、目录是否存在或者判断其是否具有某类属性(权限)的常用方法

    Conditional Logic on Files # 判断文件是否存在及文件类型 -a file exists. #文件存在 -b file exists and is a block speci ...

  9. bash Shell 中如何实现条件判断之if判断

    http://blog.51cto.com/lovelace/1211353 bash中如何实现条件判断?条件测试类型:    整数测试    字符测试    文件测试 一.条件测试的表达式:     ...

随机推荐

  1. 01.JAVA语言基础相关解答

    1.首先,第一个问题谈到一个java类文件中真的只能有一个公有类吗?并给出了以下代码.可以看出这个程序里有两个public类: 我进行了不同的调试:                           ...

  2. flutter:Could not download kotlin-gradle-plugin.jar

    我们可以看到是flutter的外部依赖:kotlin-gradle-plugin-1.2.71 下载不了,下面我介绍使用手动下载的方式去解决: 首先,看到flutter项目目录,注意我标出的是运行项目 ...

  3. Spring Cloud微服务安全实战_3-6_API安全机制之审计

    审计日志 定义:谁,在什么时间,干了什么事. 位置:认证之后,授权之前. 这样就知道是谁在访问,拒绝掉的访问也能被记录.如果放在认证之前,那么就不知道是谁在访问:如果放在授权之后,就没办法记录被拒绝的 ...

  4. RaxML使用

    1.下载 https://github.com/stamatak/standard-RAxML 2.How many Threads shall I use? 重要的是要知道,RAxML PThrea ...

  5. jQuery的animate()方法做一个颜色的渐变

    需求:在1秒内,由一个颜色变到另一个颜色,不是1秒后再变色. <!DOCTYPE html> <html lang="en"> <head> & ...

  6. [LeetCode] 897. Increasing Order Search Tree 递增顺序查找树

    Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root o ...

  7. [LeetCode] 235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  8. [LeetCode] 209. Minimum Size Subarray Sum 最短子数组之和

    Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...

  9. [LeetCode] 33. Search in Rotated Sorted Array 在旋转有序数组中搜索

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  10. 黑苹果MacOS安装记录

    https://blog.daliansky.net/macOS-Catalina-10.15-19A583-Release-version-with-Clover-5093-original-ima ...