Skip to content

Instantly share code, notes, and snippets.

@Eyakub
Last active August 10, 2017 17:03
Show Gist options
  • Save Eyakub/ff16b08cccf2af75fda6ddfab027c0f5 to your computer and use it in GitHub Desktop.
Save Eyakub/ff16b08cccf2af75fda6ddfab027c0f5 to your computer and use it in GitHub Desktop.
Program using linux Shell of my Course Operating System
Here all of mine code is from my course.
To help my classmate and to store my shell program to use from anywhere
i'm using this GIST.
#!/bin/bash
#function to add two numbers
add()
{
x=$1
y=$2
sum=`expr $x + $y`
echo -e "Number entered by u are: $x and $y"
echo "sum of $1 and $2 is $sum "
}
#function of substract two number
sub()
{
x=$1
y=$2
sum=`expr $x - $y`
echo -e "Number You Entered are: $x and $y"
echo "Sum of $1 and $2 is $sum "
}
#function of multiply two number
mul()
{
x=$1
y=$2
sum=`expr $x \* $y`
echo -e "Number you entered are: $x and $y"
echo "Sum of $1 and $2 is $sum "
}
#function of divide into two number
div()
{
x=$1
y=$2
sum`expr $x / $y`
echo -e "Number you entered are: $x and $y"
echo "Sum of $1 and $2 is $sum "
}
# main scripti
echo -e "1.Add\n2.Sub\n3.Mul\n4.Div"
echo "Enter first number"
read first
echo "Enter second number"
read sec
echo -n "Enter a value to do an operation:"
read n
if [ $n -eq 1 ]; then
add $first $sec
elif [ $n -eq 2 ]; then
sub $first $sec
elif [ $n -eq 3 ]; then
mul $first $sec
elif [ $n -eq 4 ]; then
div $first $sec
else
echo "Error"
fi
#!/bin/bash
echo "Enter random number's"
read -a arr
length=${#arr[@]}
#initial value of positive and negative
pos=0
neg=0
for((i=0;i<length;i++))
do
#checking that it's neg or not
if((${arr[$i]}<0))
then
neg=`expr $[neg+1]`
else
pos=`expr $[pos+1]`
fi
done
echo "Number of Negative number is: " $neg
echo "Number of Positive number is: " $pos
#include <iostream>
using namespace std;
int main()
{
int at[10], bt[10], wt[10], tat[10];
int n;
float avgwt = 0, avgtat = 0;
cout << "Input how many process" << endl;
cin >> n;
//taking burst time
cout << "Enter burst time of each process sequentially" << endl;
for( int i = 0; i < n; i++){
cout << "For process " << i+1<< ": ";
cin >> bt[i];
}
//calculating waiting time
wt[0]=0;
for( int i = 1; i <n; i++){
wt[i]=0;
for( int j = 0; j < i; j++){
wt[i] += bt[j];
}
//cout << "Waiting time: " << wt[i] << endl;
}
cout << "Process\tBurst Time\tWaiting time\tTurnAround Time" << endl;
for(int i = 0; i < n; i++){
avgwt += wt[i];
tat[i] = bt[i] + wt[i];
avgtat += tat[i];
cout << "p"<<i+1<<"\t"<<bt[i]<<"\t\t\t"<<wt[i]<< "\t\t "<< tat[i]<<endl;
}
avgwt /= n;
avgtat /= n;
cout << "Average waiting time: " << avgwt << endl;
return 0;
}
#!/bin/bash
echo "Enter a number to get the factorial: "
read n
fac=1
if (( fac < 0 ))
then
echo "Factorial of a negative number doesn't exist."
else
#4=3*3*1
for((i=1;i<=n;i++))
do
fac=`expr $[fac*i]`
done
echo "Factorial of $n is: " $fac
fi
#!/bin/bash
#number difference 10
echo "Enter a nubmer"
read n
m=`expr $n / 10`
case "$m" in
8|9|10 ) echo "A+" ;;
7 ) echo "A" ;;
6 ) echo "B" ;;
5 ) echo "fail" ;;
* ) echo "wrong input"
esac
#!/bin/bash
#number difference 5
echo "Enter a number to check grade"
read n
m=`expr $n / 5`
case $m in
16 ) echo "A";;
17) echo "A+";;
15 ) echo "A-";;
14 ) echo "B";;
13 ) echo "B-";;
12|11|10|9|8|7|6|5|4|3|2|1) echo "Fail";;
*) echo "Wrong input"
esac
#!/bin/bash
echo "Enter a value to check Leap year or not"
read a
if [ `expr $a % 400` == 0 ]; then
echo "$a is leap year"
elif [ `expr $a % 100` == 0 ]; then
echo "$a is leap year"
elif [ `expr $a % 4` == 0 ]; then
echo "$a is leap year"
else
echo "$a is not Leap year"
fi
#!/bin/bash
echo "Don't press enter, input value with a single space every time."
read -a arr
echo "Length of the array: " ${#arr[@]}
#storing the length of the array
length=${#arr[@]}
#echo "the length is: " $length
#for i in ${arr[@]}
#do
# echo $i
#done
echo
echo "Enter a value to search: "
read a
flag=0
pos=0
for((i=0;i<length;i++))
do
if ((${arr[$i]}==$a))
then
flag=`expr $[flag+1]`
#storing the position
pos=$i+1
break
fi
done
if (($flag==1))
then
echo "The current position of the value is: " $[pos]
else
echo "Not found"
fi
#!/bin/bash
echo "Only for doc, sh, txt, c, cpp file"
echo "Enter a file name: "
read f1
echo "Enter extension: "
read ex1
echo "Enter destination: "
read d1
echo "1. Move\n2. Copy\n3. Move folder\n4. Copy folder"
read choice
if [ $choice == 2 ]; then
if [[ ($ex1 == 'doc') || ($ex1 == 'sh') ||( $ex1 == 'txt') || ($ex1 == 'c') || ($ex1 == 'cpp') ]]; then
cp $f1.$ex1 $d1
fi
elif [ $choice == 1 ]; then
if [[ ($ex1 == 'doc') || ($ex1 == 'sh') ||( $ex1 == 'txt') || ($ex1 == 'c') || ($ex1 == 'cpp') ]]; then
mv $f1.$ex1 $d1
fi
elif [ $choice == 3 ]; then
mv $f1 $d1
elif [ $choice == 4 ]; then
cp -r $f1 $d1
fi
#for Example
#1
#2 2
#3 3 3
#!/bin/bash
echo "Pyramid"
read x
for ((i=1;i<=x;i++))
do
for ((j=1;j<=i;j++))
do
echo -n $i " "
done
echo
done
#!/bin/bash
echo -n "Enter a number to check prime or not: "
read n
i=2
while [ $i -lt $n ]
do
if [ `expr $n % $i` -eq 0 ]
then
echo "$n is not a prime number"
echo "Cause it's divisible by $i"
exit
fi
i=`expr $i+1`
done
echo "$n is a Prime number."
***********while loop**********
#!/bin/bash
echo "Print 1-10 using While loop"
i=1
while [ $i -le 10 ]
do
echo $i
#i=$((i+1))
i=`expr $i + 1`
done
************UNTILL LOOP*********
#!/bin/bash
echo "Untill loop"
i=1
until [ $i -gt 10 ]
do
echo $i
i=`expr $i + 1`
done
echo "Another loop"
a=0
until [ ! $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done
********FOR LOOP**********
#!/bin/bash
echo "For loop"
for ((i=1;i<=10;i++))
do
echo $i
done
echo
echo
#used to get the list of that directory file
echo "Another forloop"
echo
for i in `ls`
do
echo $i
done
echo
#different style for loop
echo "another for loop"
echo
for a in {1..10..2}
do
echo $a
done
#!/bin/bash
makePyramid()
{
#here $1 is the parameter you passed with the function
n=$1;
#outer loop is for printing number of rows in the pyramid
for((i=1;i<=n;i++))
do
#this loop print spaces required
for((k=i;k<=n;k++))
do
echo -ne " "
done
#this loop print part2 of the pyramid
for((j=1;j<=i;j++))
do
echo -ne "*"
done
#this loop print part 2 of the pyramid
for((z=1;z<i;z++))
do
echo -ne "*";
done
#this echo used for printing new line
echo;
done
}
#change number according to your need
echo "Enter a value of pyramid"
read n
makePyramid $n
#!/bin/bash
echo "Star printing"
#fixed the value of a row
i=5
#Get value from user of a Column
read n
for ((j=1;j<=n;j++))
do
for ((k=1;k<=i;k++))
do
echo -n "* "
done
echo
done
#!/bin/bash
echo "Don't enter untill You've done with entering value with one space"
echo "Like: 2 3 4 1"
read -a arr
#storing the length of array
length=${#arr[@]}
#initial value of sum
sum=0
for((i=0;i<length;i++))
do
#storing every array value into a temp variable to add with sum
temp=${arr[$i]}
sum=`expr $[sum+temp]`
done
avg=$(echo "$sum / $length" | bc -l)
echo "Sum: " $sum
echo "Average: " $avg
#this way
# 1 4 9 16
# 1 4 9
# 1 4
# 1
#it was increasing the way from 3+2=5+2=7+2=9 like this so y=3 and adding +2
#!/bin/bash
echo "Enter row number"
read a
for((i=1;i<=a;i++))
do
x=1
y=3
for((j=a;j>=i;j--))
do
echo -n "$x "
x=`expr $x + $y`
y=`expr $y + 2`
done
echo
done
#!/bin/bash
#when initialize get into 5, it will exit, doesn't matter the loop is done or not :D
echo "Break use"
a=1
while [ $a -le 10 ]
do
echo $a
a=`expr $a + 1`
if [ $a -eq 5 ]
then
break
fi
done
#!/bin/bash
echo "Continue"
a=1
while [ $a -le 10 ]
do
#wil check if a = 5 else it will go 16, if 5 thn continue without printing the value.
if [ $a -eq 5 ];
then
a=`expr $a + 1`
continue
fi
echo $a
a=`expr $a + 1`
done
#!/bin/bash
#2+4+6+....+n
i="y"
while [ $i == "y" ]
do
sum=0
echo "Enter a value"
read n
m=2
echo "Number series"
while [ $m -le $n ]
do
echo "$m"
sum=`expr $sum + $m`
m=`expr $m + 2`
done
echo "Result: $sum"
echo "wanna continue?? press y"
read i
if [ $i != "y" ]
then
exit
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment