Chinaunix首页 | 论坛 | 博客
  • 博客访问: 210409
  • 博文数量: 145
  • 博客积分: 3000
  • 博客等级: 中校
  • 技术积分: 1720
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-14 18:42
文章分类

全部博文(145)

文章存档

2011年(1)

2009年(144)

我的朋友

分类: LINUX

2009-09-09 09:36:07

by tangke 2009-09-09

1.变量:用$+“变量名"表示。例:$name,但是一般的标准做法是${name}来表示变量。
2.引号:
""双引号,双引号里面如果包含变量,不会求值,例如:echo "$name" 输出是$name
''单引号,单引号里面如果包含变量会求值,例如:name="JanusLe" echo '$name'则输出JanusLe
``反引号,反引号内可包含命令,运行时候会先求反引号内的命令的结果,比如: cmd=`ls` 则可能输出a.c b.c(结果和运行ls命令一样)
\斜杠 ,有些字符可能是BASH的关键字,不能直接输出,要用的话加\。比如要输出句号可以这样\. 当然也可以用单引号或者双引号包含。也可以达到效果
3.参数:
  脚本可以接受输入参数。类似于C,第一的参数是$1,依此类推。$#表示参数数目.
4.变量名赋值:
  A = "hello world" 错误
  A="hello world" 正确
  也就是说变量名赋值的时候=两边不能有空格.
5.if then else fi
  if statement
  then
  AAAAAAAAAAAAAAAAAAAA
  else
  BBBBBBBBBBBBBBBBBBBB
  fi

  或者我们可以采用
  if statement; then
  AAAAAAAAAAAAAAAAAAAA
  else
  BBBBBBBBBBBBBBBBBBBB
  fi

  在使用if [ test -e /usr/bin/ls ]; then
  这里需要注意[]两边的空格。

6. \c
clear
echo "Hello $USER"
echo "Today is \c ";date
echo "Number of user login : \c" ; who | wc -l
echo "Calendar"
cal
exit 0


7. 如何当作计算器使用
$echo `expr 6 + 3`
$echo `expr 6 / 3`
$echo `expr 6 \* 3`

8. $?
(1) If return value is zero (0), command is successful.
(2) If return value is nonzero, command is not successful or some sort of error executing command/shell script.
Simple, to determine this exit Status you can use $? special variable of shell.

9. the read statement

echo "Your first name please:"
read fname
echo "Hello $fname, Lets be friend!"

10. ;
如果两个很短的命令的话,可以使用;进行分开
$ data; who

11. $#, $*, $@
$#表示参数个数
$*
$@

12. && ||
command1 && command2
如果命令command1 返回0[执行正确], 那么执行command2

command1 || command2
如果命令command1 返回1 [执行错误], 那么执行command2

Example:
$ rm myf && echo "File is removed successfully" || echo "File is not removed"

If file (myf) is removed successful (exist status is zero) then "echo File is removed successfully" statement is executed, otherwise "echo File is not removed" statement is executed (since exist status is non-zero)

13. cat > file
cat > mf
this is my file
^D (press CTRL + D to save file)
或者
cat > mf <hello world
EOF

14. sed中'和"的区别
theme=hello
sed -i '/name.*=.*/name = ${theme}' filename
name 直接等于${theme}
sed -i "/name.*=.*/name = ${theme}" filename
name 等于hello

阅读(291) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~