我的第一个shell,if语句

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
if0else1
帖子: 94
注册时间: 2010-01-04 16:48

我的第一个shell,if语句

#1

帖子 if0else1 » 2010-01-10 22:13

#! /bin/bash
qqz2="123"
echo "input:"
read qqz
if
test $qqz -eq $qqz2
then
echo "hello"
fi


有点问题:为什么 代码写成

代码: 全选

if test $qqz -eq $qqz2 then

代码: 全选

if [test $qqz -eq $qqz2] then
就运行不了……
在if语句里,什么情况下 写成 if [条件] then 呢?
:em20
人生没有if...else
头像
xzap
帖子: 256
注册时间: 2006-08-24 21:25

Re: 我的第一个shell,if语句

#2

帖子 xzap » 2010-01-10 23:05

好像什么情况下都不能写成

代码: 全选

if test $qqz -eq $qqz2 then
因为需要换行,如果一定要写在一行上加分号;

代码: 全选

if test $qqz -eq $qqz2 ;then
头像
qlhn
帖子: 736
注册时间: 2006-08-25 10:37
联系:

Re: 我的第一个shell,if语句

#3

帖子 qlhn » 2010-01-20 9:32

路过,学习。
头像
yuguo1988
帖子: 27
注册时间: 2010-01-09 15:34
联系:

Re: 我的第一个shell,if语句

#4

帖子 yuguo1988 » 2010-01-20 15:56

#! / bin / sh
# Pairs of variable assignments:
a = "hello world"

# Print the variable a value:
echo "A is:" $ a :em11
丑要饭的玩IT。
maitianhust
帖子: 3
注册时间: 2010-01-20 21:46

Re: 我的第一个shell,if语句

#5

帖子 maitianhust » 2010-01-20 22:25

[ ]
test
是两种条件测试的用法,不能混合用。
头像
keky
帖子: 231
注册时间: 2007-12-20 15:08
来自: harbin
联系:

Re: 我的第一个shell,if语句

#6

帖子 keky » 2010-01-21 11:36

[] 其实就是代表了条件语句中的test了、呵呵。好好在看看书吧,建议你可以上鸟哥的网站去学习学习

linux.vbird.org
Keep trying...never say never. blog: H。U。C

代码: 全选

        .--.
       |o_o |
       |:_/ |           < Hello Girl >
      //   \ \             ------------
     (|     | )
    /'\_   _/`\
    \___)=(___/
tusooa
帖子: 6548
注册时间: 2008-10-31 22:12
系统: 践兔
联系:

Re: 我的第一个shell,if语句

#7

帖子 tusooa » 2010-01-23 22:24

代码: 全选

>> help if
if: if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COMMANDS; ] fi
    Execute commands based on conditional.

    The `if COMMANDS' list is executed.  If its exit status is zero, then the
    `then COMMANDS' list is executed.  Otherwise, each `elif COMMANDS' list is
    executed in turn, and if its exit status is zero, the corresponding
    `then COMMANDS' list is executed and the if command completes.  Otherwise,
    the `else COMMANDS' list is executed, if present.  The exit status of the
    entire construct is the exit status of the last command executed, or zero
    if no condition tested true.

    Exit Status:
    Returns the status of the last command executed.
if a ; then b ; [ elif c ; then d ; ] ... [ else e ; ] fi
如果if或者elif后面的命令执行成功(return 0)就执行then后面的东东,否则继续往下看。

判断某命令是否执行成功是if ...而不是if [ ... ]

代码: 全选

] ls -ld //
头像
luofeng1989
帖子: 766
注册时间: 2009-09-20 19:30
系统: ubuntu12.04
来自: GIS

Re: 我的第一个shell,if语句

#8

帖子 luofeng1989 » 2010-01-23 23:05

打酱油的学习。。。 :em06
famingyuan
帖子: 18
注册时间: 2009-06-01 22:31

Re: 我的第一个shell,if语句

#9

帖子 famingyuan » 2010-01-29 10:54

test 和 [ 是相同的,不能同时写在一起。

if 和 then 要么不再同一行,要么 就用 ;隔开。貌似是语法问题。
nico86
帖子: 2
注册时间: 2010-02-07 17:48

Re: 我的第一个shell,if语句

#10

帖子 nico86 » 2010-02-07 18:00

if [ $qqz -eq $qqz2 ];then
echo "qqz = qqz2"
fi
回复