help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-bash] How to use "&&" to replace "if" statement when "return" is u


From: Peng Yu
Subject: [Help-bash] How to use "&&" to replace "if" statement when "return" is used?
Date: Sun, 21 Apr 2013 09:19:07 -0500

Hi,

I'm trying to use "&&" to replace "if". But the following shows that
when "return" is used the replacement can not be done. Is there a way
to use "&&" to replace "if" even "return" or "exit" is used?

~/linux/test/bash/man/operator/&&/return$ cat.sh *
==> main.sh <==
#!/usr/bin/env bash

set -v
function cmd1 {
[ '' ] && return 1
}

cmd1
echo $?

function cmd2 {
[ x ] && return 1
}

cmd2
echo $?

function cmd1 {
if [ '' ]
then
  return 1
fi
}

cmd1
echo $?

function cmd2 {
if [ x ]
then
  return 1
fi
}

cmd2
echo $?


==> main_echo.sh <==
#!/usr/bin/env bash

set -v
function cmd1 {
[ '' ] && echo 1
}

cmd1

function cmd2 {
[ x ] && echo 1
}

cmd2

function cmd1 {
if [ '' ]
then
  echo 1
fi
}

cmd1

function cmd2 {
if [ x ]
then
  echo 1
fi
}

cmd2

~/linux/test/bash/man/operator/&&/return$ ./main.sh
function cmd1 {
[ '' ] && return 1
}

cmd1
echo $?
1

function cmd2 {
[ x ] && return 1
}

cmd2
echo $?
1

function cmd1 {
if [ '' ]
then
  return 1
fi
}

cmd1
echo $?
0

function cmd2 {
if [ x ]
then
  return 1
fi
}

cmd2
echo $?
1

~/linux/test/bash/man/operator/&&/return$ ./main_echo.sh
function cmd1 {
[ '' ] && echo 1
}

cmd1

function cmd2 {
[ x ] && echo 1
}

cmd2
1

function cmd1 {
if [ '' ]
then
  echo 1
fi
}

cmd1

function cmd2 {
if [ x ]
then
  echo 1
fi
}

cmd2
1



-- 
Regards,
Peng



reply via email to

[Prev in Thread] Current Thread [Next in Thread]