Advertisement
arfin97

Shell Scripting - Basics, Variable, ifelse,loop

Jun 28th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.05 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #Input out put name and birth year: Learning outcome - basic input output and variable
  4.  
  5. echo "hello world"
  6.  
  7. echo "what is your name?"
  8. read name
  9. echo "hello, $name"
  10.  
  11. echo "enter your birth year: "
  12. read n
  13.  
  14. #Even odd checking: Learning out come: Basic if, else structure
  15.  
  16. #Write a shell script to find whether a number is even or odd.
  17. #Sample input: 4
  18. #Sample output : even
  19.  
  20. if (( $n%2 == 0 ))
  21. then
  22.     echo "the number is even"
  23. else
  24.     echo "the number is odd"
  25. fi
  26.  
  27.  
  28. #Take an integer input. Show the pyramid with stars(*) in output
  29.  
  30. #printing the name 10 times using loop: Learning outcome - Basic loop syntax
  31. for (( i=0; i<=10; i++))
  32. do
  33.     echo "my name is $name"
  34. done
  35.  
  36.  
  37.  
  38.  
  39. #printing pyramid: Learning outcome - Loops and nested loops
  40.  
  41.  
  42. echo -n "enter pyramid size: "
  43. read p
  44.  
  45. echo -n "enter pyramid character: "
  46. read ch
  47.  
  48. for(( i=0; i<p; i++))
  49. do
  50.     for(( j=i; j<p; j++))
  51.     do
  52.         echo -n " "
  53.     done
  54.    
  55.     for(( k=0; k<=i; k++))
  56.     do
  57.         echo -n "$ch"
  58.     done
  59.  
  60.     for(( l=0; l<i; l++))
  61.     do
  62.         echo -n "$ch"
  63.     done
  64.     echo ""
  65. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement