论坛首页 综合技术论坛

Bash编程学习笔记(二)

浏览 2334 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-10-16  
读写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-desktop$echo $name

yaoyuan-desktop$name=yaoyuan
yaoyuan-desktop$echo $name
yaoyuan
yaoyuan-desktop$echo $place

yaoyuan-desktop$echo ${name:-John} ${place:-Portland}
yaoyuan Portland
yaoyuan-desktop$echo ${place?"Not defined"}
bash: place: Not defined
yaoyuan-desktop$echo ${name:+"Not defined"}
Not defined
yaoyuan-desktop$echo ${place:+"Not defined"}

yaoyuan-desktop$echo ${place:="San Francisro"}
San Francisro
yaoyuan-desktop$echo ${name:-John} ${place:-Portland}
yaoyuan San Francisro



'',"",*,\的使用

yaoyuan-desktop$name=yaoyuan
yaoyuan-desktop$echo $name
yaoyuan
yaoyuan-desktop$name=yao yuan
bash: yuan:找不到命令
yaoyuan-desktop$echo $name
yaoyuan
yaoyuan-desktop$name=yaoyuan*
yaoyuan-desktop$echo $name
yaoyuan
yaoyuan-desktop$name=yaoyuan
yaoyuan-desktop$echo $name
yaoyuan
yaoyuan-desktop$echo "$name"
yaoyuan*
yaoyuan-desktop$echo "Is Not Hello World! "
Is Not Hello World! 
yaoyuan-desktop$echo '$name'
$name
yaoyuan-desktop$echo '$name'
$name




Shell命令替换

$(command)

yaoyuan-desktop$command=pwd
yaoyuan-desktop$echo "The value of command is: $command."
The value of command is: pwd.
yaoyuan-desktop$command=$(pwd)
yaoyuan-desktop$echo "The value of command is: $command."
The value of command is: /home/yaoyuan.

命令替换适用于任何命令

yaoyuan-desktop$echo "The date and time is $(date)."
The date and time is 2008年 10月 12日 星期日 01:41:28 CST.



创建只读的用户自定义的变量

declare -r [name-list]
typeset -r [name-list]
readonly [name-list]

用于阻止对name-list中列出的变量赋新值

yaoyuan@yaoyuan-desktop:~/ScriptFile$ declare -r name=Yao place=Yuan
yaoyuan@yaoyuan-desktop:~/ScriptFile$ echo $name $place
Yao Yuan

yaoyuan@yaoyuan-desktop:~/ScriptFile$ name=Yuan
bash: name: 只读变量
yaoyuan@yaoyuan-desktop:~/ScriptFile$ place="YaoYuan"
bash: place: 只读变量


只读变量的值不能重设
yaoyuan@yaoyuan-desktop:~/ScriptFile$ unset name
bash: unset: name:无法重置:只读的 variable
yaoyuan@yaoyuan-desktop:~/ScriptFile$ unset place
bash: unset: place:无法重置:只读的 variable


从标准输入设备读入

read [options][variable-list]

用于从标准输入设备读入一行,把读入的词赋给在variable-list中的变量

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








论坛首页 综合技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics