Sponsored Content
Top Forums Shell Programming and Scripting [newb] simple script, big problem Post 302492513 by itomb on Monday 31st of January 2011 06:26:17 PM
Old 01-31-2011
[newb] simple script, big problem

Warning! I'm ridiculously new at all this, so pardon my ignorance...

I have a very simple script which is intended to search a hosts file when given a partial hostanme or ip address. The if the partial hostname/ip given is unique, the script automatically logs the user in to that host. If there are multiple matches, a numerical list of all matches is presented as a menu. Selecting the number that represents the intended host then logs the user in.

The script works like a charm if the results (the menu) only shows the host name or the ip address. I want it to show both. However, when it shows both, the login fails.


The menu portion of the script is as follows:

Code:
ip=$1
 test=$(cat /etc/hosts | grep -c $ip)
            if [ $test -eq 1 ]; then
                clogin `awk '/'"$ip"'/ {print $2}' /etc/hosts `
                    else
                    options=$"`awk '/'"$ip"'/ {print $1 "--"  $2}' /etc/hosts`"
                    echo
                    echo "There were $test matches for '$ip' found"
                    echo
                    echo "Please select the site you would like to login to:"
                    echo
                    select opt in $options;do
                    echo " "
                    clogin $opt
                    exit 1
                    done


When run, the result is as expected:
Code:
./test springs

There were 3 matches for 'springs' found

Please select the site you would like to login to:

1) 10.1.1.2--hostname_with_springs1
2) 10.1.1.3--hostname_with_springs2
3) 10.1.1.4--hostname_with_springs3

Of course, then, selecting any of those fails because of the dashes. The menu is delimted via white-space so, having any spaces between the IP and hostname would create a menu selection for each (giving six matches to the above query). Smilie

How can I have my cake and eat it too?

Thank you in advance for any help on this!

Last edited by Scott; 01-31-2011 at 11:21 PM.. Reason: Please use code tags
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Simple ksh script problem

This is a search script that I have developed. It finds the records that I look for. However the only thing I want the program to do now is to display a message such as 'Not Found' to appear when a record is not found. So far when a search doesn't display a record, the screen is blank. ... (14 Replies)
Discussion started by: Warrior232
14 Replies

2. UNIX for Dummies Questions & Answers

simple shell script problem

hi all. i have a little problem. im basically reading input from the user from the keyboard into the variable "phonenumber". I want to do a little error check to check if the user doesnt enter anything in for the value phonenumber. i had this: read phonenumber if then ..... else ........ (2 Replies)
Discussion started by: djt0506
2 Replies

3. Shell Programming and Scripting

Simple script problem

Hi everyone - I am sure this is a really simple problem but I'm a total noob at Linux scripting: I wanted to create a script that allows me to compare the current week number to the contents of a text file in my home directory: VAR1='date +%V' VAR2='cat /home/fred/file.txt' ... (6 Replies)
Discussion started by: FiniteRed
6 Replies

4. Shell Programming and Scripting

one simple shell script problem

Hi everyone, I am facing to one shell script problem, which is as following Write a shell script that: Takes a number of arguments. For each argument, print out all files in the current directory that contain this substring in their name. I know I need to use grep for the second... (7 Replies)
Discussion started by: shaloovia
7 Replies

5. Shell Programming and Scripting

Simple AWK script problem.

Hi all, I have set up a simple awk script to calculate the average of values that are printed out a number of times per second (the number of time the printing occurs varies). The data is of the format shown below: 1 4.43 1 3.65 1 2.45 2 7.65 2 8.23 2 5.65 3 4.65 3 6.21 .. .. 120... (4 Replies)
Discussion started by: omnomtac
4 Replies

6. Shell Programming and Scripting

problem writing a simple c shell script

#!/bin/csh echo hello world this is what i got in a text file called ss1. i type "chmod 755 ss1.txt" to make it executable. then when i type ss1 or ss1.txt it says "ss1 command not found" what am i doing wrong? (19 Replies)
Discussion started by: pantelis
19 Replies

7. UNIX for Dummies Questions & Answers

simple script with while loop getting problem

Hello forum memebers. can you correct the simple while program. #! /bin/ksh count=10 while do echo $count count='expr$count-1' done I think it will print 10 to 1 numbers but it running for indefinite times. (2 Replies)
Discussion started by: rajkumar_g
2 Replies

8. Shell Programming and Scripting

Simple bash script problem

#!/bin/bash cd /media/disk-2 Running ./run.sh it's not changing directory.Why? (6 Replies)
Discussion started by: cola
6 Replies

9. Shell Programming and Scripting

Shell Script (simple problem)

I want to find and replace string from files present in one directory. user will input the string to be searched and to replace . Here is my program but Not working echo "Enter Old domain name:" read old echo "Enter New domain name:" read new grep -rl '$old' /var/www/ | xargs sed -i... (4 Replies)
Discussion started by: sunny2802
4 Replies

10. Shell Programming and Scripting

Bash - sftp simple script problem

Hello, when running the scripts below I am not getting message bb2. Can you please help? #!/bin/bash TLOG=/tmp/bb/amatest.log FTPRESULTS=/tmp/bb/amlist export TLOG FTPRESULTS >$TLOG >$FTPRESULTS echo bb1 sftp -oPort=2222 XXXXXXXXXXXXX@sftp.userssedi.com <<EOF cd... (5 Replies)
Discussion started by: biljana
5 Replies
script(1)							   User Commands							 script(1)

NAME
script - make record of a terminal session SYNOPSIS
script [-a] [filename] DESCRIPTION
The script utility makes a record of everything printed on your screen. The record is written to filename. If no file name is given, the record is saved in the file typescript. See WARNINGS. The script command forks and creates a sub-shell, according to the value of $SHELL, and records the text from this session. The script ends when the forked shell exits or when Control-d is typed. OPTIONS
The following option is supported: -a Appends the session record to filename, rather than overwriting it. NOTES
script places everything that appears on the screen in filename, including prompts. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWcsu | +-----------------------------+-----------------------------+ |CSI |Enabled | +-----------------------------+-----------------------------+ SEE ALSO
attributes(5) WARNINGS
script can pose a security risk when used in directories that are writable by other users (for example, /tmp), especially when run by a privileged user, that is, root. Be sure that typescript is not a link before running script. SunOS 5.10 30 Jan 2004 script(1)
All times are GMT -4. The time now is 01:52 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy