bash scripts(4)

本文通过多个示例详细介绍了在Shell脚本中如何进行变量赋值。包括基本赋值、使用let命令、for循环中的赋值、read命令赋值及通过执行命令来赋值等方法。此外还展示了使用$(...)机制进行变量赋值的新方法。
摘要由CSDN通过智能技术生成

Example 5-2. Plain Variable Assignment

#!/bin/bash

echo

# When is a variable "naked", i.e., lacking the '$' in front?
# When it is being assigned, rather than referenced.

# Assignment
a=879
echo "The value of "a" is $a"

# Assignment using 'let'
let a=16+5
echo "The value of "a" is now $a"

echo

# In a 'for' loop (really, a type of disguised assignment)
echo -n "The values of "a" in the loop are "
for a in 7 8 9 11
do
  echo -n "$a "
done

echo
echo

# In a 'read' statement (also a type of assignment)
echo -n "Enter "a" "
read a
echo "The value of "a" is now $a"

echo

exit 0

Example 5-3. Variable Assignment, plain and fancy

#!/bin/bash

a=23              # Simple case
echo $a
b=$a
echo $b

# Now, getting a little bit fancier...

a=`echo Hello!`   # Assigns result of 'echo' command to 'a'
echo $a

a=`ls -l`         # Assigns result of 'ls -l' command to 'a'
echo $a

exit 0

Variable assignment using the $(...) mechanism (a newer method than backquotes)

# From /etc/rc.d/rc.local
R=$(cat /etc/redhat-release)
arch=$(uname -m)
[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/8503278/viewspace-890158/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/8503278/viewspace-890158/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值