# Basic shell script program in Unix/Linux.

- Link: https://technotaught.com/basic-shell-script-program-in-unix-linux/
- Published: 2019-03-03T20:01:47+05:30
- Author: Ashishkumar Vishwakarma

The Basic shell script [program](https://technotaught.com/basic-networking-command-in-linux-unix/)
which is used in [Unix](https://technotaught.com/study-of-general-commands-of-ubuntu/).

#### 1. Printing count of arguments passed by command line

    ```language-git
    echo $#
    exit 0
    ```

> rock@rock-VirtualBox:~$ bash file.sh 1 2 3
>  3 rock@rock-VirtualBox:~$ bash file.
> sh 1 2 3 4 5  5

#### 2. Accept Age from user and if user enter 18-60 age then give message ELIGIBLE FOR VOTING and else INVALID

    ```language-git
    echo Enter the Age:
    read age
    if (( $age >=18 && $age <=60 ))
    then
    	echo "ELIGIBLE FOR VOTING"
    else
    	echo "INVALID"
    fi
    ```

> rock@rock-VirtualBox:~$ bash file.sh
>  Enter the Age: 13 INVALID rock@rock-VirtualBox:
> ~$ bash file.sh Enter the Age: 37 ELIGIBLE FOR VOTING

#### 3. Accept a number from user and test whether it is “POSITIVE” or “ZERO” and “NEGATIVE”.

    ```language-git
    echo Enter the no:
    read no
    if (( $no > 0 ))
    then
    	echo POSITIVE NO
    elif(( $no == 0))
    then
    	echo ZERO
    else
    	echo NEGATIVE
    fi
    ```

> rock@rock-VirtualBox:~$ bash file.sh
>  Enter the no: 5 POSITIVE NO rock@rock-VirtualBox:
> ~$ bash file.sh Enter the no: -1 NEGATIVE rock@rock-VirtualBox:~$ bash file.sh
> Enter the no: 0 ZERO

[Refer Basic command](https://technotaught.com/basic-networking-command-in-linux-unix/)

#### 4. Ask user how many times to print hello

Using while loop

    ```language-git
    echo "Enter no:"
    read n
    echo ---
    i=1
    while (( $i <= n))
    do
    	echo $i. Hello
    	((i++))
    done
    ```

> rock@rock-VirtualBox:~$ bash file.sh
>  Enter no: 5—1. Hello2. Hello3. Hello4. Hello5.
> Hello

Using for loop

    ```language-markup
    echo "Enter no:"
    read n
    echo ---

    for (( i=1; i<=n;i++))
    do
    	echo $i. Hello

    done
    ```

> rock@rock-VirtualBox:~$ bash file.sh
>  Enter no: 5
> —
>  1. Hello 2. Hello 3. Hello 4. Hello 5. Hello

using util loop

    ```language-markup
    echo "Enter no:"
    read n
    echo ---
    i=1
    until (( $i > $n))
    do
    	echo $i. Hello
    	((i++))
    done
    ```

> rock@rock-VirtualBox:~$ bash file.sh
>  Enter no: 5— 1. Hello 2. Hello 3. Hello 
> 4. Hello 5. Hello

#### 5. Check whether user entered number is Prime or not.

 * The **expr** command used to evaluate a given expression and display its corresponding
   output.
 * Used for basic operations like addition, subtraction, multiplication, division
   and modulus on integers.

    ```language-git
    echo Enter number:
    read number
    i=2 
    f=0 
    while test $i -le `expr $number / 2` 
    do
    	if test `expr $number % $i` -eq 0 
    	then
    		f=1 
    	fi
    	i=`expr $i + 1` 
    done
    if test $f -eq 1 
    then
    	echo "Not Prime"
    else
    	echo "Prime"
    fi
    ```

    ```language-markup
    echo Enter number:
    read number
    i=2 
    f=0 
    while [ $i -le `expr $number / 2` ]
    do
    	if [ `expr $number % $i` -eq 0 ]
    	then
    		f=1 
    	fi
    	i=`expr $i + 1` 
    done
    if [ $f -eq 1 ]
    then
    	echo "Not Prime"
    else
    	echo "Prime"
    fi
    ```

 

> rock@rock-VirtualBox:~$ bash file.sh
>  Enter number: 4 Not Prime rock@rock-VirtualBox:
> ~$ bash file.sh Enter number: 57 Not Prime

[ Commands in Ubuntu](https://technotaught.com/study-of-general-commands-of-ubuntu/)

[Vi editor commands](https://technotaught.com/command-of-the-vi-editor-and-implement-them/)

#### 6. Fibonacci series.

    ```language-git
    echo "enter the no:"
    read no
    a=0
    b=1
    echo "Fibonacci series upto $no:"
    for(( i=0; i<no; i++))
    do
        echo  "$a"
        c=$((a+b)) 
        a=$b
        b=$c
    done
    ```

> $ bash file.sh
>  enter the no: 7 Fibonacci series upto 7: 0 1 1 2 3 5 8

#### 7.Check whether user entered number is EVEN or ODD.

    ```language-git
    echo Enter no:
    read n
    if [ $((n%2)) -eq 0]
    then
           echo "$n is even"
    else
           echo "$n is odd" 
    fi
    ```

>  
> rock@rock-VirtualBox:~$ bash file.sh Enter number: 4 4 is even rock@rock-VirtualBox:
> ~$ bash file.sh Enter number: 57 57 is odd

### Share this:

 * [  X ](https://technotaught.com/basic-shell-script-program-in-unix-linux/?share=twitter)
 * [  Facebook ](https://technotaught.com/basic-shell-script-program-in-unix-linux/?share=facebook)
 * [More](https://technotaught.com/basic-shell-script-program-in-unix-linux/#)

 * [  WhatsApp ](https://technotaught.com/basic-shell-script-program-in-unix-linux/?share=jetpack-whatsapp)
 * [  LinkedIn ](https://technotaught.com/basic-shell-script-program-in-unix-linux/?share=linkedin)
 * [  Email ](https://technotaught.com/basic-shell-script-program-in-unix-linux/?subject=%5BShared%20Post%5D%20Basic%20shell%20script%20program%20in%20Unix%2FLinux.&body=https%3A%2F%2Ftechnotaught.com%2Fbasic-shell-script-program-in-unix-linux%2F&share=email)
 * [  Reddit ](https://technotaught.com/basic-shell-script-program-in-unix-linux/?share=reddit)
 * [  Tumblr ](https://technotaught.com/basic-shell-script-program-in-unix-linux/?share=tumblr)
 * [  Pinterest ](https://technotaught.com/basic-shell-script-program-in-unix-linux/?share=pinterest)
 * [  Telegram ](https://technotaught.com/basic-shell-script-program-in-unix-linux/?share=telegram)
 * [  Print ](https://technotaught.com/basic-shell-script-program-in-unix-linux/#print?share=print)

### _Related_

## Comments

### By: Michelle B

- Link: https://technotaught.com/basic-shell-script-program-in-unix-linux/#comment-135
- Published: 2020-04-24T00:28:04+05:30

Nice

### By: quit 9 to 5 academy reviews

- Link: https://technotaught.com/basic-shell-script-program-in-unix-linux/#comment-89
- Published: 2020-01-08T18:42:18+05:30

This article is very useful to me. Thanks for posting it.
