Skip to main content

UNIX Programs


1. WAS: to display 1 to 10 numbers (while loop)

$ cat >f5
a=1
while [ $a -le 10 ]
do
echo "$a"
a=`expr $a + 1`
done

2. WAS: to display 1 to 10 numbers (for loop)

$ cat >f5
for (( a=1;a<=10;a++ ))
do
echo "$a"
done
  
3. WAS: to display 1 to 10 numbers (until loop)

$ cat >g1
a=1
until [ $a -gt 10 ]
do
echo $a
a=`expr $a + 1`
done




4. WAS: to display 10 to 1 numbers (while loop)

$ cat >g2
a=10
while [ $a -ge 1 ]
do
echo $a
a=`expr $a - 1`
done  

5. WAS: to display 10 to 1 numbers (for loop)

$ cat >f5
for (( a=10;a>=1;a-- ))
do
echo "$a"
done

6. WAS: to display 10 to 1 numbers (until loop)

$ cat >f6
a=10
until [ $a -lt 1 ]
do
echo $a
a=`expr $a - 1`
done   

7. WAS: to display 1 to N numbers (while loop)

echo "Upto what numbers you want"
read n
a=1
while [ $a -le $n ]
do
echo $a
a=`expr $a + 1`
done        




8. WAS: to display 1 to N numbers (for loop)

echo "Upto what numbers you want"
read n
for (( a=1;a<=n;a++ ))
do
echo “$a”
done

9. WAS: to display 1 to N numbers (until loop)

echo "Upto what numbers you want"
read n
a=1
until [ $a -ge $n ]
do
echo $a
a=`expr $a + 1`
done        

10. WAS: to display N to 1 numbers (while loop)

echo "enter the no"
read no
while [ $no -ge 1 ]
do
echo $no
no=`expr $no - 1`
done 

11. WAS: to display N to 1 numbers (for loop)

echo "from what numbers you want"
read n
for (( a=n;a>=1;a-- ))
do
echo “$a”
done




12. WAS: to display N to 1 numbers (Until loop)

echo "enter the no"
read no
until [ $no -le 1 ]
do
echo $no
no=`expr $no - 1`
done 

13. WAS: to find the sum of 1 to 10 numbers (while loop)

$ cat >f4
a=1
sum=0
while [ $a -le 10 ]
do
sum=`expr $sum + $a`
a=`expr $a + 1`
done
echo "total is $sum"

14. WAS: to find the sum of 1 to 10 numbers (for loop)

a=1
sum=0
for (( a=1;a<=10;a++ ))
do
sum=`expr $sum + $a`
done
echo "total is $sum"

15. WAS: to find the sum of 1 to 10 numbers (until loop)

$ cat >f4
a=1
sum=0
until [ $a -ge 10 ]
do
sum=`expr $sum + $a`
a=`expr $a + 1`
done
echo "total is $sum"
16. WAS: to find the factorial of a given no (while loop)

$ cat >g7
fact=1
echo "enter the no"
read no
while [ $no -gt 0 ]
do
fact=`expr $fact \* $no`
no=`expr $no - 1`
done
echo "factorial is $fact" 

17. WAS: to find the factorial of a given no (for loop)

fact=1
echo "enter the no"
read no
for (( no=no;no>0;no-- ))
do
fact=`expr $fact \* $no`
done
echo "factorial is $fact" 

18. WAS: to find the factorial of a given no (until loop)

$ cat >g7
fact=1
echo "enter the no"
read no
until [ $no -lt 0 ]
do
fact=`expr $fact \* $no`
no=`expr $no - 1`
done
echo "factorial is $fact" 

19. WAS: to display Fibonacci series (while loop)

a=0
b=1
echo " how much steps you want "
read no
echo $a
echo $b
x=`expr $no - 2`
while [ $x -gt 0 ]
do
c=`expr $a + $b`
echo $c
a=$b
b=$c
x=`expr $x - 1`
done     

20. WAS: to display Fibonacci series (for loop)

a=0
b=1
echo " how much steps you want "
read no
echo $a
echo $b
x=`expr $no - 2`
for (( ;x>0;x-- ))
do
c=`expr $a + $b`
echo $c
a=$b
b=$c
done

21. WAS: to display Fibonacci series (Until loop)

$ cat >fib
a=0
b=1
echo " how much steps you want "
read no
echo $a
echo $b
x=`expr $no - 2`
until [ $x -lt 0 ]
do
c=`expr $a + $b`
echo $c
a=$b
b=$c
x=`expr $x - 1`
done         

22. WAS: to get the reverse number of given digit (while loop)

$ cat >f5
echo "enter the number"
read no
while [ $no -gt 0 ]
do
x=`expr $no % 10`
echo $x
no=`expr $no / 10`
done

23. WAS: to get the reverse number of given digit (for loop)

$ cat >f5
echo "enter the number"
read no
for (( no=no;no>0;no=no/10 ))
do
x=`expr $no % 10`
echo $x
done

24. WAS: to get the reverse number of given digit (Until loop)

$ cat >f5
echo "enter the number"
read no
until [ $no -lt 0 ]
do
x=`expr $no % 10`
echo $x
no=`expr $no / 10`
done

25. WAS: to display the multiplication table of a particular number (while loop)

$ cat >mul
i=1
echo "enter the number for multiplication table"
read no
while [ $i -le 10 ]
do
x=`expr $i \* $no`
echo "$i x $no = $x"
i=`expr $i + 1`
done

26. WAS: to display the multiplication table of a particular number (for loop)

$ cat >mul
echo "enter the number for multiplication table"
read no
for (( i=1;i<=10;i++ ))
do
x=`expr $i \* $no`
echo "$i x $no = $x"
done

27. WAS: to display the multiplication table of a particular number (until loop)

$ cat >mul
i=1
echo "enter the number for multiplication table"
read no
until [ $i -ge 10 ]
do
x=`expr $i \* $no`
echo "$i x $no = $x"
i=`expr $i + 1`
done

28. WAS: to display the multiplication table from upto N number (While loop)

$ cat >muln
i=1
j=1
echo "enter upto wht number you want the tables"
read num
while [ $j -le $num ]
do
while [ $i -le 10 ]
do
x=`expr $i \* $j`
echo " $i x $j = $x"
i=`expr $i + 1`
done
j=`expr $j + 1`
done         

29. WAS: to display the multiplication table from upto N number (While loop)

$ cat >f5
echo "enter upto wht number u wnt"
read no
for (( j=1;j<=no;j++ ))
do
for (( i=1;i<=10;i++ ))
do
t=`expr $i \* $j`
echo "$i x $j = $t"
done
done  

30. WAS: to display the multiplication table from upto N number (Until loop)

$ cat >muln
i=1
j=1
echo "enter upto wht number ypu want the tables"
read num
until [ $j -ge $num ]
do
until [$i -ge 10 ]
do
x=`expr $i \* $j`
echo " $i x $j = $x"
i=`expr $i + 1`
done
j=`expr $j + 1`
done         

31. WAS: to do sum of any digit (while loop)

$ cat >sumdigit
echo "enter any no"
read no
sum=0
while [ $no -gt 0 ]
do
x=`expr $no % 10`
sum=`expr $sum + $x`
no=`expr $no / 10`
done
echo "sum = $sum"

32. WAS: to do sum of any digit (for loop)

$ cat >sumdigit
echo "enter any no"
read no
sum=0
for (( no=no;no>0;no/=10 ))
do
x=`expr $no % 10`
sum=`expr $sum + $x`
done
echo "sum = $sum"

33. WAS: to do sum of any digit (Until loop)

$ cat >sumdigit
echo "enter any no"
read no
sum=0
until [ $no -lt 0 ]
do
x=`expr $no % 10`
sum=`expr $sum + $x`
no=`expr $no / 10`
done
echo "sum = $sum"
34. Write a shell script to find even or not.
$ cat >f1
echo "enter the no"
read no
x=`expr $no % 2`
if [  $x -eq 0 ]
then
echo "even"
else
echo "odd"
fi

35. WAS. To find the given year is leap year or not

$ cat >f2
echo "enter the year"
read year
x=`expr $year % 4`
if [ $x -eq 0 ]
then
echo "leap year"
else
echo "not leap year"
fi

36. WAS: to find the greatest of all from 3 numbers.

$ cat >f3
echo "enter three numbers"
read n1
read n2
read n3
if [ $n1 -gt $n2 ] && [ $n1 -gt $n3 ]
then
echo "$n1 is greater"
elif [ $n2 -gt $n3 ] && [ $n2 -gt $n1 ]
then
echo "$n2 is greater"
else
echo "$n3 is greater"
fi
36. WAS: to find the greatest of all from 3 numbers.

$ cat >f5
i=1
max=$1
for i in $*
do
if [ $i -gt $max ]
then
max=$i
fi
done
echo "max = $max"

37. WAS: to find percentage and class of a student.

$ cat >f4
echo "enter 5 sub marks"
read s1
read s2
read s3
read s4
read s5
tot=`expr $s1 + $s2 + $s3 + $s4 + $s5`
per=`expr $tot \* 100 / 5`
if [ $per -gt 70 ]
then
echo "distinction"
elif [ $per -gt 60 ]
then
echo "first class"
elif [ $per -gt 50 ]
then
echo "second class"
else
echo "third class"
fi



38. WAS: to find it is prime or not

$ cat >prime
x=0
i=2
echo "enter the no"
read no
while [ $i -lt $no ]
do
y=`expr $no % $i`
if [ $y -eq 0 ]
then
x=1
fi
i=`expr $i + 1`
done
if [ $x -eq 0 ]
then
echo "prime"
else
echo "not prime"
fi       

39. WAS: to find string is equal or not

$ cat >equal
if test "$1" == "$2"
then
echo "equal"
else
echo "not equal"
fi   

40. WAS: to find check that file exist or not

$ cat >f2
if test -a f1
then
echo "exist"
else
echo "dosent exist"
fi    
41. WAS: to find that file is readable or not

$ cat >f1
if test -r f1
then
echo "readable"
else
echo "not readable"
fi  

42. WAS: to find that file is writable or not

$ cat >f1
if test -w f1
then
echo "writeable"
else
echo "not writeable"
fi  

43. WAS: to find that string is null or not

$ cat >f1
if test -z $1
then
echo "NULL"
else
echo "NOT NULL"
fi  

44. WAS: to find that file is executable or not

$ cat >f2
if test -x f1
then
echo "executable"
else
echo "not"
fi   



45. WAS: to find the minimum and maximum from given 5 numbers

$ cat >f3
i=1
min=$1
max=$1
for i in $*
do
if [ $i -lt $min ]
then
min=$i
fi
if [ $i -gt $max ]
then
max=$i
fi
done
echo "min = $min and max = $max"

46. WAS: to count how many even and odd numbers are there.

$ cat >f6
i=1
a=0
b=0
for i in $*
do
j=`expr $i % 2`
if [ $j -eq 0 ]
then
a=`expr $a + 1`
fi
if [ $j -eq 1 ]
then
b=`expr $b + 1`
fi
done
echo "even = $a and odd = $b"

Made By: Chaudhary Amit V.

Comments

Popular posts from this blog

Characteristics of a Good Programming Language

Till now there are many high level languages which are very popular, and there are others, which could not become so popular in-spite of being very powerful. There might be many reasons for the success of a language, but one obvious reason is the characteristics of the language. Several characteristics believed to be important with respect to making a programming language good are briefly discussed below. Simplicity A good programming language must be simple and easy to learn and use. For example, BASIC is liked by many programmers only because of its simplicity. Thus, a good programming language should provide a programmer with a clear, simple and unified set of concepts which can be easily grasped. It is also easy to develop and implement a compiler or an interpreter for a programming language that is simple. However, the power needed for the language should not be sacrificed for simplicity. The overall simplicity of a programming language strongly affects the readability of the pr...

Angular 4 and Firebase Authentication: Setup

If you have come to this article, it means you are keen to learn new technology and that too Firebase. I love Firebase because it provides all basic but important features of any web application. Take for example, authentication, which is cumbersome and risky if not implemented with utmost care. And when we have the integration of Angular and Firebase, it becomes a lot easier for a developer to build such crucial modules in less time with minimal efforts. In this article we will create a simple Angular application using Firebase. I am going ahead with the understanding that you know the benefits of Firebase and have little knowledge about it. There are plenty of posts out on the web if you are behind. Step 1: Install Angular CLI (if not installed). npm install -g angular-cli Step 2: Create new Angular 4 project. By default now angular CLI will create Angular 4 project so you need not fret. ng new firebase-authentication Step 3: Check whether the ne...

Switch-case, simple calculator . . .

11.      Write a program to perform some mathematic functions with the help of “switch-case” statement. #include<stdio.h> #include<conio.h> void main() { int a,b,ans; char c; clrscr(); printf("\ Enter a and b:"); scanf("%d%d",&a,&b); printf("\n Enter operator:"); fflush(stdin); scanf("%c",&c); switch(c) { case'+':ans=a+b;       break; case'-':ans=a-b;       break; case'*':ans=a*b;       break; case'/':ans=a/b;       break; } printf("\n ans %d",ans); getch(); } 12.      Print the Fibonacci series #include<stdio.h> #include<conio.h> void main() { int a=1,b=1,s=3,c,ch; clrscr(); printf("how many steps="); scanf("%d",&ch); printf("%5d%5d",a,b); for(s=3;s<=ch;s++) {       c=a+b;       printf("%5d",c);       a=b;    ...