LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-30-2013, 09:39 AM   #1
keif
Member
 
Registered: Apr 2013
Posts: 107

Rep: Reputation: Disabled
Promoting the user for $1 input


Good morning everyone,

I want to run a simple grep script on a remote server.

Instead of having the user type the script and then the hostname, I would like them to be prompted to type the hostname, and then use the input they typed for $1.

The example below is called grep-error.sh

Code:
#!/bin/sh

ssh root@$1 grep -i error vision.error
As it is now, in order to execute this they will have type:

Code:
./grep-error.sh hostname
Instead of doing that, I would like to set it up so after they type the script into the command prompt, it asks them for a hostname:

Code:
./grep-error.sh
Please enter a hostname:
Then I want the hostname to be passed along to $1.

Any help is appreciated. Thanks.
 
Old 08-30-2013, 09:47 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
You can use read to do this.

Simple example:
Code:
#!/bin/bash

echo -n "Please enter a hostname: "
read HOSTNAME

echo $HOSTNAME
There's no need for $1 (at least for the hostname part).

Have a look at the bash manual page (SHELL BUILTIN COMMANDS section, read).

EDIT: Example 15-3. Variable assignment, using read

Last edited by druuna; 08-30-2013 at 09:48 AM.
 
1 members found this post helpful.
Old 08-31-2013, 02:43 PM   #3
keif
Member
 
Registered: Apr 2013
Posts: 107

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by druuna View Post
You can use read to do this.

Simple example:
Code:
#!/bin/bash

echo -n "Please enter a hostname: "
read HOSTNAME

echo $HOSTNAME
There's no need for $1 (at least for the hostname part).

Have a look at the bash manual page (SHELL BUILTIN COMMANDS section, read).

EDIT: Example 15-3. Variable assignment, using read
Thanks. Worked great.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Using user input within a script pukka Linux - General 2 07-18-2013 12:10 PM
alternative to less with user input ZetaNet Programming 7 08-20-2012 01:35 PM
User Input puriandrua Linux - Newbie 4 08-15-2011 11:42 PM
User input into Bash scripts and checking validity of user input?? helptonewbie Programming 8 07-07-2008 06:40 PM
Bash Y/N user input zcrxsir88 Programming 11 04-16-2008 11:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration