读书人

Bash编程学习札记(二)

发布时间: 2012-10-05 15:34:34 作者: rapoo

Bash编程学习笔记(二)
读写Shell变量

命令替代操作符及其描述

$variable得到一个变量的值,如果没有被初始化则为Null没有被初始化返回NULL
${variable}得到一个变量的值,如果没有被初始化则为Null,通常在其他的东西要添加到变量值的时候使用同上
${variable:-string}得到一个变量的值,如果这个变量没有被定义则返回一个确定的值 variable存在而且不是空值的时候返回变量的值,否在那返回null
${variable:=string}得到一个变量没有被定义则把一个已知的值赋给它并返回这个值 variable存在不是空值的时候返回值,否则string赋给variable,返回string
${variable:?string}如果变量没有被定义则显示一条消息variable存在而且不是空值的时候,返回变量的值,否则显示字符串variable,并在其后显示message
${variable:+string}测试一个变量存在与否variable存在而且不是空值的时候返回string,否则返回null


yaoyuan@yaoyuan-desktop:~/ScriptFile$ cat read_demo#! /bin/bashecho -n "Enter input: "read lineecho "You entered: $line"echo -n "Enter another line: "read word1 word2 word3echo "The first word is: $word1"echo "The second word is : $word2"echo "The rest of the line is: $word3"exit 0yaoyuan@yaoyuan-desktop:~/ScriptFile$ ./read_demoEnter input: yaoyuanYou entered: yaoyuanEnter another line: hello shell, I love youThe first word is: helloThe second word is : shell,The rest of the line is: I love you








读书人网 >编程

热点推荐