zoukankan      html  css  js  c++  java
  • linux shell 中的位置变量

    对于linux shell 中的位置变量,我一直以来都是吐不出来又咽不下去,每次看到都不懂,不懂了就去百度google,看完了又忘,真是慢性咽炎啊。现在认真想想也是,其实自己一直以来都没有好好学习过,只是看了些速成的东西,匆匆忙忙地扫描,然后以光的速度忘掉了。好了,不淡这些了,希望和大家共勉。

    第一次在Makefile中看到位置参数这东西比如$@,$^,$<等 ,shell中的位置参数也长这样子,但含义是不一样的。在shell中

    $0 , 表明脚本的名字,比如执行./test/test.sh 那么$0=./test/test.sh

    $#,表示传递到脚本的参数的个数

    $*,$@,两者都是指所有传递到脚本的参数

    $$,这个是脚本运行的pid,跟参数好像没有关系。

    $?命令的退出状态,也就是命令结果码的保存位置。

    其实就这几样东西,不难吧。之前连位置参数这个概念都没有,自己就容易搞混淆也就不足为奇了。

    另外,$* 和 $@两者只有在被双引号括起来的时候才有区别的:

    看下在程序的运行结果:

    #!/bin/bash
    index=1
    for arg in "$@"
    	do
    		echo "arg $index=$arg"
    		let "index+=1"
    	done
    
    index=1
    
    for arg in "$*"
    	do
    		echo "arg $index=$arg"
    		let "index+=1"
    	done
    
    index=1
    for arg in $@
    	do
    		echo "arg $index=$arg"
    		let "index+=1"
    	done
    
    index=1
    for arg in $*
    	do
    		echo "arg $index=$arg"
    		let "index+=1"
    	done
    

    ./test.sh 1 2 3 4 的运行结果:

    arg 1=1
    arg 2=2
    arg 3=3
    arg 4=4
    arg 1=1 2 3 4
    arg 1=1
    arg 2=2
    arg 3=3
    arg 4=4
    arg 1=1
    arg 2=2
    arg 3=3
    arg 4=4
    

    ./test 1 "2 3" 4的结果:

    arg 1=1
    arg 2=2 3
    arg 3=4
    arg 1=1 2 3 4
    arg 1=1
    arg 2=2
    arg 3=3
    arg 4=4
    arg 1=1
    arg 2=2
    arg 3=3
    arg 4=4
    
  • 相关阅读:
    hdu 2492 树状数组 Ping pong
    HDU 1532 基础EK Drainage Ditches
    EK算法模板
    Codeforces Round #538 (Div. 2) (A-E题解)
    Codeforces Global Round 1 (A-E题解)
    Educational Codeforces Round 59 (Rated for Div. 2) DE题解
    Codeforces Round #535 (Div. 3) 题解
    Codeforces Round #534 (Div. 2) D. Game with modulo(取余性质+二分)
    POJ2253:Frogger(改造Dijkstra)
    POJ1797:Heavy Transportation(改造Dijkstra)
  • 原文地址:https://www.cnblogs.com/mosmith/p/4148780.html
Copyright © 2011-2022 走看看