#!/bin/bash # Function to perform logical AND operation logical_and() { result=$(( $1 && $2 )) } # Function to perform logical OR operation logical_or() { result=$(( $1 || $2 )) } # Function to perform logical NOT operation logical_not() { result=$(( !$1 )) } # Function to perform logical XOR operation logical_xor() { result=$(( ($1 || $2) && !($1 && $2) )) } # Function to perform conditional AND operation conditional_and() { result=$(( $1 & $2 )) } # Function to perform conditional OR operation conditional_or() { result=$(( $1 | $2 )) } # Function to perform conditional NOT operation conditional_not() { result=$(( ~$1 )) } # Function to perform addition addition() { result=$(( $1 + $2 )) } # Function to perform subtraction subtraction() { result=$(( $1 - $2 )) } # Function to perform multiplication multiplication() { result=$(( $1 * $2 )) } # Function to perform division division() { result=$(( $1 / $2 )) } # Function to find remainder remainder() { result=$(( $1 % $2 )) } # Parse command line arguments num1=$1 num2=$2 operation=$3 # Perform the selected operation case $operation in "+") addition $num1 $num2 ;; "-") subtraction $num1 $num2 ;; "*") multiplication $num1 $num2 ;; "/") division $num1 $num2 ;; "%") remainder $num1 $num2 ;; "&&") logical_and $num1 $num2 ;; "||") logical_or $num1 $num2 ;; "!") logical_not $num1 ;; "^^") logical_xor $num1 $num2 ;; "&|") conditional_and $num1 $num2 ;; "|") conditional_or $num1 $num2 ;; "~") conditional_not $num1 ;; *) echo "Invalid operation" && exit 1 ;; esac # Display the result echo "Result: $result"
Write, Run & Share Bash code online using OneCompiler's Online Bash Shell for free. It's one of the robust, feature-rich Bash shell available over online and getting started with the OneCompiler's Bash Shell is simple and pretty fast. The editor shows sample boilerplate code when you choose language as Bash
. OneCompiler also has reference scripts, where you can look for the sample scripts and start coding.
Bash (Bourne Again Shell) is a shell program written by Brian Fox and is an upgraded version of Bourne Shell program 'sh'.
name="Foo"
echo $name
echo "$name"
echo "${name}"
if [ conditional-expression ]
then
statements
fi
if [ conditional-expression ]
then
statements
else
statements
fi
if [ conditional-expression ]
then
statements
elif [ conditional-expression ]
then
statements
else
statements
fi