read X
read Y
echo $((X+Y))
echo $((X-Y))
echo $((X*Y))
echo $((X/Y))
Input
5
2
Ouput
7
3
10
2
This type of for loop is characterized by counting. The range is specified by a beginning (#1) and ending number (#5). The for loop executes a sequence of commands for each member in a list of items.
A representative example in BASH is as follows to display welcome message 5 times with for loop:
#!/bin/bash
for i in 1 2 3 4 5
do
echo "Welcome $i times"
done
Sometimes you may need to set a step value (allowing one to count by two's or to count backwards for instance). Latest bash version 3.0+ has inbuilt support for setting up ranges:
#!/bin/bash
for i in {1..5}
do
echo "Welcome $i times"
done
Bash v4.0+ has inbuilt support for setting up a step value using {START..END..INCREMENT} syntax:
#!/bin/bash
echo "Bash version ${BASH_VERSION}..."
for i in {0..10..2}
do
echo "Welcome $i times"
done
While loops iterate "while" a given condition is true. An example of this:
#!/bin/bash
X=0
while [ $X -le 20 ]
do
echo $X
X=$((X+1))
done
Comparisons in a shell script, may either be accomplished using regular operators (such as < or >) or using (-lt, -gt, -eq i.e. lesser than, greater than, equal to) for POSIX shells.
read X
read Y
if [ $X -gt $Y ]
then
echo "X is greater than Y"
elif [ $X -eq $Y ]
then
echo "X is equal to Y"
else
echo "X is lesser than Y"
fi
can use ge
(greater than or equal to) and le
(less than or equal to)
Tags: <conditional>
, <and>
, <or>
Read in one character from the user (this may be 'Y', 'y', 'N', 'n'). If the character is 'Y' or 'y' display "YES". If the character is 'N' or 'n' display "NO". No other character will be provided as input. Your task:
Read in one character from the user (this may be 'Y', 'y', 'N', 'n'). If the character is 'Y' or 'y' display "YES". If the character is 'N' or 'n' display "NO". No other character will be provided as input.
Input Format One character (this may be 'Y', 'y', 'N', 'n').
Output Format One word: either "YES" or "NO" (quotation marks excluded)
read input
if [ $input == "Y" ] || [ $input == "y" ]
then
echo "YES"
else
echo "NO"
fi
We provide you with expressions containing +,-,*,^, / and paranthesis. None of the numbers in the expression involved will exceed 999. Your task, is to evaluate the expression and display the output correct to 3 decimal places.
Input 1
5+50*3/20 + (19*2)/7
Output 1
17.929
read expression
echo "scale=5; $expression" | bc -l | awk '{printf "%.3f", $1}'
Given N integers, compute their average, correct to three decimal places.
Input Format The first line contains an integer N. This is followed by N integers, each on a new line.
Output Format Display the average of the N integers, correct to three decimal places.
Input Constraints 1 <= N <= 500 -10000 <= x <= 10000 (x refers to elements of the list of integers for which the average is to be computed)
read n
sum=0
for (( i = 0; i < n; i++ )); do
read x
sum=$((sum + x))
done
echo $sum $n | awk '{printf "%.3f", $1 / $2}'
cut -c-4
cut -f2-
=> Input
a b d
a c e
=> Output
b d
c e
cut -d' ' -f-3
=> Input
New York is a state in the Northeastern and Mid-Atlantic regions of the United States.
New York is the 27th-most extensive, the third-most populous populated of the 50 United States.
New York is bordered by New Jersey and Pennsylvania to the south.
About one third of all the battles of the Revolutionary War took place in New York.
Henry Hudson's 1609 voyage marked the beginning of European involvement with the area.
=> Output
New York is
New York is
New York is
About one third
Henry Hudson's 1609
cut -b2,7
=> Input
Hello
World
how are you
=> Output
e
o
oe
head [filename] # First 10 lines
head -n 11 [filename] # First 11 lines
head -c 20 [filename] # First 20 characters
tail [filename] # Last 10 lines
tail -n 11 [filename] # Last 11 lines
tail -c 20 [filename] # Last 20 characters
echo "Hello" | tr "e" "E"
=> 'e' being transformed to 'E'
echo "Hello how are you" | tr " " '-'
=> spaces being transformed to hyphens
echo "Hello how are you 1234" | tr -d [0-9]
=> digits (numerals) being deleted
echo "He llo Wor ld how are you" | tr -s " "
=> replace all sequences of multiple spaces with just one space
grep PATTERN
grep -n PATTERN # Prefix each line of output with the 1-based line number
### Clipboard | |
```sh | |
xclip -selection clipboard -o | |
``` |
More Conditional
shell.sh