Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8734261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:48:26+00:00 2026-06-13T09:48:26+00:00

In the header of my bash script, I have to ask for the user

  • 0

In the header of my bash script, I have to ask for the user name and his IP address.

I need to validate entries in this way:

  • The user name have to be stored in a name variable, having his space replaced by underscores. For sample if user hit "Albert Smith", I have to store "Albert_Smith" into my variable name.
  • For the ip address, I need to know if the ip address is consistent with the format.
    If the format is not good, I must report the error and prompt the user to enter a good ip address to continue the operation.

I know that i need to use “read”, but i don’t know to realize my short script (replace and verify).
Thank in advance 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:48:27+00:00Added an answer on June 13, 2026 at 9:48 am

    I suppose your entries will be line based (ie. the name will be the first line and the IP the second). You can do this:

    #!/bin/bash
    
    read name    
    while [[ ! "$name" =~ '[A-Za-z ]' ]]; do
        read -p "Wrong name format. Re-enter: " name
    done
    
    name="${name// /_}"
    
    read ip
    while [[ ! "$ip" =~ '^((25[0-5]|2[0-4][0-9]|[01][0-9][0-9]|[0-9]{1,2})[.]){3}(25[0-5]|2[0-4][0-9]|[01][0-9][0-9]|[0-9]{1,2})$' ]]; do
        read -p "Not an IP. Re-enter: " ip
    done
    

    The IP regex is rather complex, but it can be decomposed as:

    1. After the start of the string, there must be three groups of “octet” followed by periods, then followed by another “octet” and the end of the string
    2. Octet is either
      1. 250-255,
      2. 200-249
      3. 100-199 (including 000-099, with the explicit 0s before the number)
      4. One or two digits (ie. 0-99)

    The loop forms will negate ! the result of the grep, so it will continue looping while the grep command fails. We use printf in order to pass the variable into grep. The -E flag on the second grep allows the use of extended regular expressions, which include the | OR operator, () grouping and {} repetitions.

    Hope this helps a little =)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this script: #!/bin/bash CLASSPATH=/blah/libs/*:/blah/more/libs CMD=java -cp $CLASSPATH MainClass ALREADY_RUNNING_PID=`ps -ef --no-headers |
I'm looking for a way to create a switch for this bash script so
hi all i need to write a simple bash script that lists all PHP
I have the following snippet in a bash script: db2 connect to $DB var=$(db2
I have a bash script which use curl: url=https://example.com/api.cgi message=<?xml version=\1.0\ encoding=\utf-8\?> <request> <encoding>utf-8</encoding>
Possible Duplicate: grep loses coloring when run from bash script I have a simple
I have a bash script, that logs in with a cookie and saves a
I am writing a bash script that needs to get the header (first 10
I'm trying to write a bash script which will, given the name of a
I have the followign bash script to update mtimes for maildir files: #!/bin/bash for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.