BASH shell script question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting BASH shell script question
# 1  
Old 05-22-2002
BASH shell script question

I want to make shell script that takes a list of host names on my network as command line arguments and displays whether the hosts are up or down, using the ping command to display the status of a host and a for loop to process all the host names. Im new to shell scripting so Im not quite sure where to start with this. Thanks for any help -E
# 2  
Old 05-23-2002
Well, this sounds kind of like a small assignment (homework, or real work), but I think we can get you on the right path...

Check the manual page (man bash)... Check into using a "for" loop...

for variable in "list of servers"
do
ping $variable
(do some other stuff to see if it worked)
done

If you work on the script a little, I'm sure someone could help you more if you post problem areas...
# 3  
Old 05-23-2002
Im almost there, but not quite

Heres what I got going so far:

PHP Code:
#!/bin/bash
###############################
# 15.11
# Check host shell script
#
#
#
#
###############################

#Get our list of hosts

echo -"Enter hosts: "
        
read hosts
checkhost
()
{
  echo 
"Checking host $1..."
  
ping -c 2 $&> /dev/null

  
if [ $? == ]
  
then
    
echo "$1 is running"
  
else
    echo 
"$1 is not reachable"
  
fi
}

HOSTLIST="$hosts"

for hosts in $HOSTLIST
do
  
checkhost $hosts
done 
I got some help from another forum but I'm still not getting a result. I always get host not reachable. Im fairly certain its a problem with the hosts/hostlist vars, Im missing something somewhere. -E
# 4  
Old 05-24-2002
Close...

Whoever did this for you, or suggested the use of function is making things more difficult that they need to be... Here's a script following similar flow that should work:
Code:
#!/bin/bash
# The \c at the end tells it not to
# return to the next line... just for looks

echo -e "What hosts shoud I check: \c"
read hosts

# What ever you type in at the end is read
# into a variable named hosts
# Now I'll use "each" in hosts, instead of 
# trying to reassign $hosts

for each in $hosts
do
# send all output away... rely on the reutrn code instead
ping -c 2 $each >/dev/null 2>&1
# if the return is Not Equal to 0 (successful)...
if [ "$?" -ne "0" ]; then
echo "Host $each is not reachable! "
fi
done

This one works for me...
Check the man page for test and for bash.
If you don't understand something in the script, please post back.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed

In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed ? Is there any way to get the script names for the process command ? --- Post updated at 08:39 AM --- in KSH (Korn Shell), my command output shows the script names but when run in the Bash Shell... (3 Replies)
Discussion started by: i4ismail
3 Replies

2. Shell Programming and Scripting

Different behavior between bash shell and bash script for cmd

So I'm trying to pass certain json elements as env vars and use them later on in a script. Sample json: JSON='{ "Element1": "file-123456", "Element2": "Name, of, company written in, a very weird way", "Element3": "path/to/some/file.txt", }' (part of the) script: for s... (5 Replies)
Discussion started by: da1
5 Replies

3. Shell Programming and Scripting

Bash shell script undefined array item value question

Hello, I'm new here. I test these expressions's value in my script : (in centOS 6 ) #!/bin/bash array='something' echo "############" echo ${array} echo ${array} echo ${array} echo "############" The output result is : ################# something something #################... (5 Replies)
Discussion started by: lingjing
5 Replies

4. Shell Programming and Scripting

Question in bash script.

Hi All, I need an assistance with the issue below. I wrote big script in "bash" that automatically install an LDAP on Clients. I'd be happy to know in order to avoid duplication of entries in files, How i can define into the script, if the specific expressions already exist in the file, do... (7 Replies)
Discussion started by: Aviel.shani
7 Replies

5. Shell Programming and Scripting

Bash script - loop question

Hi Folks, I have a loop that goes through an array and the output is funky. sample: array=( 19.239.211.30 ) for i in "${array}" do echo $i iperf -c $i -P 10 -x CSV -f b -t 50 | awk 'END{print '$i',$6}' >> $file done Output: 19.239.211.30 19.2390.2110.3 8746886 seems that when... (2 Replies)
Discussion started by: nitrohuffer2001
2 Replies

6. Solaris

bash shell send script question

Hi Experts, Need your help. I am trying to send a command via ssh to about a hundred network devices. I intend to do this via a bash script something similar to the below: ssh -l user testmachine.com "show version" Obviously this will not work given the password prompt that comes... (2 Replies)
Discussion started by: marcusbrutus
2 Replies

7. Shell Programming and Scripting

Quick question: calling c-shell script from bash

Hello, I have a quick reference question: I have a very long, but fairly straigtforward script written in c-shell. I was wondering if it is possible to call this script from bash (for ex. having a function in bash script which calls the c-shell script when necessary), and if so, are there any... (1 Reply)
Discussion started by: lapiduslost
1 Replies

8. Shell Programming and Scripting

bash script question

Can anybody be kind to explaing me what the lines below mean ? eval line=$line export $line ] || echo $line (2 Replies)
Discussion started by: jville
2 Replies

9. Shell Programming and Scripting

BASH script question

Hi, I want to create a script that gets a filename as an argument. The script should generate a listing in long list format of the current directory, sorted by file size. This list must be written to a new file by the filename given on the command line. Can someone help me with this? ... (6 Replies)
Discussion started by: I-1
6 Replies

10. Shell Programming and Scripting

one question for bash shell script

Just one question for bash shell script. In bash script, you can use *.txt to call any files in current folder that ends with .txt, like rm *.txt will remove all txt file in current folder. My question is can you actually remember or use the file name among *.txt, I know file=*.txt will not... (9 Replies)
Discussion started by: zx1106
9 Replies
Login or Register to Ask a Question