Shell scripting - which word is in alphabetical order?

How can you check which words are first alphabetically between two words? For example, in code

#!/bin/bash var1="apple" var2="bye" if [ $var1 \> $var2 ] then echo $var1 else echo $var2 fi 

I want it to print an apple, since the apple appears earlier in alphabetical order, but it does not work properly. What am I doing wrong?

+4
source share
3 answers

What you need to do to solve the immediate problem is to change the meaning of your statement, since the operator is less than < , not > .

This way it will work correctly:

 if [ $var1 \< $var2 ] 

Alternatively, you can use the [[ , which does not require escaping:

 if [[ $var1 < $var2 ]] 

I prefer the latter because:

  • he looks better; and
  • option [[ much more expressive and effective.
+7
source

You want to use the [[ ]] construct and print one that is smaller than the other

 #!/bin/bash var1="apple" var2="bye" if [[ $var1 < $var2 ]]; then echo $var1 else echo $var2 fi 
+4
source

It seems that you are suffering from two misconceptions about shell scripts (bash).

First, the line if [ $var1 > $var2 ] then echo ... is syntactically incorrect, and you really should paste your sample commands or code, rather than trying to retype them from memory. It would be nice if you said if [[ "$var1" > "$var2" ]]; then ... if [[ "$var1" > "$var2" ]]; then ... or if [ "$var" \> "$var2" ]; then ... if [ "$var" \> "$var2" ]; then ...

Note that [[ is a bash conditional expression, and [(single bracket) represents the built-in shell implementation of the /usr/bin/[ command (alias for /usr/bin/test ).

The old [ ( test ) command has much more limited functions than support [[ in bash. It only supports -lt , -eq ... and other integer comparisons and various file lengths and values ​​( -z' and -n ) and other tests. It has no support for lexical/string or pattern (regex nor glob) comparisons. The ) and other tests. It has no support for lexical/string or pattern (regex nor glob) comparisons. The ) and other tests. It has no support for lexical/string or pattern (regex nor glob) comparisons. The bash built-in for [ supports a number of the [[`extensions, but, as shown, some of them should be explicitly escaped from the previous analysis.

Also note that it is dangerous to use $var table layouts (vs "$var" with quotes). If the value assigned to var has any built-in spaces or various other operators that can be combined with the switches of the test command.

Also you need ; separate the if command from the then clause.

Trying to write shell scripts as if the shell was an ordinary programming language, you will encounter such confusion. Cases such as bash have relatively little syntax and built-in functions, and most of them are gluing around running commands. In earlier versions of UNIX, the shell did not have built-in tests and relied entirely on the external test command. Over time, more and more functions were built into the shell, often through aliases to those old commands ( /usr/bin/[' is literally a link to the / usr / bin / test command and the shell built-ins for [ and test ] are internal aliases to each other and are implemented as (basically?) compatible with old (and still existing) external binaries.

Similarly, all arithmetic operations in the early Bourne shells were performed using external commands such as /usr/bin/expr . Shell Korn and bash added $ ((...)) and let and ((...)) expressions to evaluate arithmetic expressions inside the shell without external command support.

Other examples relate to support for arrays ( declare ) and the extension of the ${var#...} parameter to various other forms.

As a rule, it is better to avoid most of these functions or use them sparingly, as the resulting scripts, because they are gradually less portable when you use them ... and syntax manipulations quickly overload the code. At some point it is best to use Perl, Python, Ruby, or some common programming language / scripting language to do general programming work and use the shell for the purpose for which it was developed ... like glue around external commands, for marshaling data and variables to and from external commands / processes.

+1
source

Source: https://habr.com/ru/post/1392584/


All Articles