- Array
- Copy files from folder
- This script copies files from one git directory from another, to help simplifying git repositories
- Loop Curl Cli Shell Linux
chmod u+x script.sh
./script.sh
read -p "a:" a; read -p "b:" b; echo $((a + b))
- Reading input:
read -p "a:" a
: Prompts the user to input a value for variablea
. The-p
option allows you to specify a prompt string ("a:") that is displayed to the user.read -p "b:" b
: Prompts the user to input a value for variableb
. The-p
option specifies the prompt string ("b:") that is displayed to the user.
- Performing arithmetic and printing the result:
echo $((a + b))
: Performs the arithmetic operation of adding the values ofa
andb
, and then prints the result. The$((...))
syntax is used for arithmetic expansion in the shell, which allows for performing calculations.
read -p "a:" a; read -p "b:" b; read -p "o:" o; echo `expr $a $o $b`
- Reading input: (Refer to the explanation above)
read -p "a:" a
read -p "b:" b
read -p "o:" o
: Prompts the user to input a value for variableo
, which is expected to be a mathematical operator (e.g.,+
,-
,*
,/
). The-p
option specifies the prompt string ("o:") that is displayed to the user.
- Performing arithmetic and printing the result:
echo \
expr $a $o $b`: Performs the arithmetic operation specified by the operator
oon the values of
aand
b, and then prints the result. The backticks ``
...`` are used for command substitution, where the expression inside the backticks is executed, and its output is returned. The
exprcommand evaluates the expression
$a $o $b`.
To handle floating-point arithmetic and avoid issues with certain operators, we can use bc
(a command-line calculator):
read -p "a:" a; read -p "b:" b; read -p "o:" o; echo "$a $o $b" | bc
- Reading input: (Refer to the explanation above)
read -p "a:" a
read -p "b:" b
read -p "o:" o
- Performing arithmetic and printing the result:
echo "$a $o $b" | bc
: Constructs the arithmetic expression and pipes it tobc
for evaluation. This method handles floating-point numbers and more complex arithmetic operations.
for val in {1..5}; do echo $val; done
# Reads input and sets it in variable 'a'
read -p "input number: " a
# Checks if both 'a' and 'b' are set
if [ -n "$a" -a -n "$b" ]; then
echo "Both 'a' and 'b' are set."
fi
# Checks if either 'a' or 'b' is set
if [ -n "$a" -o -n "$b" ]; then
echo "Either 'a' or 'b' is set."
fi
# Checks if 'a' is empty
if [ -z "$a" ]; then
echo "'a' is empty."
fi
# Checks if the number of arguments is not equal to 2
if [ $# -ne 2 ]; then
echo "The number of arguments is not equal to 2."
fi
# The symbol $# is the number of arguments
# The symbol -ne stands for "not equal"
# Evaluates expression
echo $(expr $a $operand $b)
# Reads two inputs and evaluates their sum
read -r x
read -r z
echo $(expr $x + $z)