T
T
Taiserisa2021-02-07 16:40:51
linux
Taiserisa, 2021-02-07 16:40:51

Bash Script is not working correctly, why?

The script is very simple, rewritten from a book for practice:

#!/bin/sh
#
# 
#
echo "script4 - Linux Scripting Book"

if [ $# -ne 4 ] ; then
    echo "Usage: script4 number1 number2 number3 number4"
    echo " Please enter 4 numbers."
exit 255
fi

echo Parameters: $1 $2 $3 $4

echo Showing logical AND
if  ; then       # logical AND
    echo Clause 1
else
    echo Clause 2
fi

echo Showing logical OR
if  ; then         # logical OR
    echo Clause 1
else
    echo Clause 2
fi
echo "End of script4"
exit 0


If you set the parameters 1 1 1 1, then in theory it should issue Clause 1 and Clause 1. And it gives out this:
601fec5a60a2a837159661.jpeg

Ubuntu 20.04.1. on the virtual machine. Script from a 2017 book. What could be the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
xotkot, 2021-02-07
@Taiserisa

the script is working

$ bash --version
GNU bash, версия 5.1.4(1)-release (x86_64-pc-linux-gnu)
...
$ ./untitled.sh 1 1 1 1
script4 - Linux Scripting Book
Parameters: 1 1 1 1
Showing logical AND
Clause 1
Showing logical OR
Clause 1
End of script4

perhaps it's in the encoding or in something else
.
try replacing sh with bash at the beginning of the file
#!/bin/bash

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question