Find the answer to your Linux question:
Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By atreyu
I'm very new to script writing and programing and I'm stuck. I would like to know if I'm on the right path? I need to write a shell script that ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2013
    Posts
    1

    simple script problem


    I'm very new to script writing and programing and I'm stuck. I would like to know if I'm on the right path?

    I need to write a shell script that will prompt the user for a single integer and display twice its value. Then do a preliminary evaluation of the user's input and repeat its numeric value before displaying the result of the operation. (I.e., check for non-numeric or non-integer input and inform the user)
    what I have so far is this:

    Code:
    #!/bin/sh
    
    echo "enter an integer"
    read integer
    integer=(($integer*2))
    
    if [ $integer=  ] ;
    then
    echo $integer
    else 
    echo "error your input is not an integer"
    I think I have the first part correct. but for the if-then-else section, i'm unsure of how to complete the test condition. what do I use to prove if the input is an integer?
    Last edited by atreyu; 04-18-2013 at 07:56 PM. Reason: added CODE tags to aid in readability

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Quote Originally Posted by wings1 View Post
    I'm very new to script writing and programing and I'm stuck. I would like to know if I'm on the right path?

    I need to write a shell script that will prompt the user for a single integer and display twice its value. Then do a preliminary evaluation of the user's input and repeat its numeric value before displaying the result of the operation. (I.e., check for non-numeric or non-integer input and inform the user)
    what I have so far is this:

    Code:
    #!/bin/sh
    
    echo "enter an integer"
    read integer
    integer=(($integer*2))
    
    if [ $integer=  ] ;
    then
    echo $integer
    else 
    echo "error your input is not an integer"
    I think I have the first part correct. but for the if-then-else section, i'm unsure of how to complete the test condition. what do I use to prove if the input is an integer?
    you are missing your ending "fi".

    there are a million ways to check for a number. i like to use grep.

    also, i usually put the "read" part in a loop, to make sure something is entered. if i want it to match on something, i put that in the loop, too. here's an example:
    Code:
    #!/bin/bash
    
    until [ -n "$input" ]; do
      echo -n "enter an integer: "
      read input
      if [ -n "$input" ]; then
        echo $input|grep ^[0-9][0-9]*$
        if [ $? -ne 0 ]; then
          echo "$input: input is not a number"
          unset input
        fi
      fi
    done
    
    integer=$(( $input * 2 ))
    
    echo Input integer is $input
    echo Integer doubled is $integer
    Lakshmipathi likes this.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •