Bash c-style if instruction and styling methods

From what I understand, you can do C-style for while and while loops in bash.

LIMIT=10

for ((a=1; a <= LIMIT ; a++))  # Double parentheses, and "LIMIT" with no "$".
do
  echo -n "$a "
done                           # A construct borrowed from 'ksh93'.

And even triple operators.

(( var0 = var1<98?9:21 ))

How do you do this using the instructions if?

Also why don't they do bindings like in C? What is the logic of the use of these key words, such as done, do, ifand fi? I will write some scripts, but bash looks very different.

Are there any bash or bash styling methods for alternatives / plugins? I would like to follow the standard, but from the background of C, Java and PHP, bash looks really weird. Utility and standards are important.

+6
source share
5 answers

bash (, , sh), if:

  • if if condition-command ; then list ; fi ( elif else ).

    • condition-command , bash ( , ), . ( , . .)
    • Bash ( Unix ) ( C): 0 ( " " ), ( " " ). ( C : 0 - false, - true.)
    • condition 0 (true), list ( if ). 0 (false), ( elif else, ). ( , if 0.)
  • ( while loops):

    • [ ... ] test ... - , 0 (true) 1 (false) , . ( .)
    • [[ ... ]] - . , [, , , <, > (... ) , buildin. , 0 (true) 1 (false).
    • (( ... )) ( ) . 0 (true), () 0 1 (false), () 0. ( let ....)

      $, ( , , ). :

      • : ++, --, +, -, !, ~ ( C)
      • : ** (), *, /, %, +, -
      • -: <<, >>
      • : <=, >=, <, >, ==, != ( , 1 (true), 0 (false))
      • : &, ^, |
      • : &&, || , , : ... ? ... : ...
      • : =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |=
      • : , ( , )

      , C . ( , . . info '(bash)Shell Arithmetic'.)

    • true - buildin, 0.
    • false - buildin, 1.
    • C ( - ) , .
  • :

    • ( ... ) - , . .
    • { ...; } - , .
    • ; - , .
    • && , , 0.
    • || , , -0.
    • & - , ( ).

, if , then fi:

if (( i > 0 ))
then {
   echo "i > 0"
}
else {
   echo "i <= 0"
}
fi

, then, else fi () . ( ; .)

, , , C.

+12

bash if , (), , bash

if [[ ... ]];then
  ..
fi 

if [ ... ];then
 ...
fi

if $(grep .. file) ;then  #note the () is part of $().
  ...
fi

bash, IMO, , , /, Python, Ruby Perl. Bash , .. , , * nix "" script, . "" , Ruby/Python. IMO, , , , .

+1

. , , Bourne (sh). Bourne Algol C.

C- ( csh), - .

, .

, Tiny C Compiler, C script.

#!/usr/bin/tcc -run
#include <stdio.h>

int main()
{
    printf("Hello World\n");
    return 0;
}
+1

- bash, , Ruby, Python PHP, script bash script. bash one-liners , , , .

ARGV .

, , Ruby script #!/usr/bin/ruby, :

#!/usr/bin/ruby
for a in 1..10
  puts ( a >= 5 ) ? "a >= 5" : "a < 5"
end
0

&& ||

true, false test 0 -eq 0 , (( i > 0 ))

true && {
  echo "true command returns zero (aka success)"
  # more commands here
  # and here
} || {
  echo "true command never returns non-zero"
  # more commands here
  # and here
}

# I don't like this one so much
false && echo "false never returns zero" || echo "false returns always 1"

# here the draw-back are back slashes
test 0 -eq 0 \
  && echo "Surprise, zero is equal to zero" \
  || echo "Apocalypse is near if zero is not zero"

NOTE : I can claim that the methods described above work, but I do not know any performance flaws, if any.

0
source

All Articles