Problems with past arguments in script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problems with past arguments in script
# 8  
Old 05-19-2015
Thank you
# 9  
Old 05-19-2015
So, did a bit additional fun, it now parses ip addr to figure out the base_ip dynamicly.
Also, output is a bit changed:
Code:
localping 1-20
Pinging 192.168.10.1... is up
Pinging 192.168.10.2... is down

You're welcome, enjoy Smilie
Code:
#!/bin/bash
#
#	Variables
#
	[ -z "$1" ] && echo "Usage: ${0##*/} 15 30-37 42 .."
	TMP=~/.cache/tmp.$$
#
# 	Prepare Base IP
#
	# IPv6? -- dont work, not needed, but fun anyway.
	[ "-6" = "$1" ] && \
		filter=6 && \
		shift || filter=" "
	# Get Ip
	ip addr|grep inet"$filter" |grep -ve 127 -ve " ::"|awk '{print $2}' > $TMP
	while IFS="." read A B C igno; do IP_PRE="$A.$B.$C"; done<$TMP
#
#	Ping arguments
#
	for num in "${@}";do	# Parse all the arguments
		# Its a range, as it contains a '-'
		if [ "-" = "$(echo $num|tr -d [[:digit:]])" ]
		then	# So lets get the start and end range
			range_start=${num/-*}
			range_end=${num/*-}
			# Lets fil lthe variable with the range of IP's
			range=$(seq $range_start 1 $range_end)
		else	# It has no range, its a single number
			range=$num
		fi

		# Parse all the numbers of range, and use them for the ping command
		for r in $range;do
			# Let the user know what we're doing now...
			printf "%s" "Pinging $IP_PRE.$r... "
			# Execute the command and append the missing text according to exit status
			ping -c 1 $IP_PRE.$r 2>/dev/zero  1>/dev/zero  && \
				echo "is up" || \
				echo "is down"
		done
	done
#
#	Cleanup
#
	rm -f $TMP

# 10  
Old 05-20-2015
@sea, just for the fun of it, some comments:
- use ping6 to ping IPv6 addresses
- There's only one IP_PRE, so the while loop might be redundant. maybe:
Code:
IP_PRE=$(ip addr | awk -vFLT=" " '/ 127\./ {next} $0 ~ "inet" FLT {sub (/[^.]*$/, "", $2); print $2}')

- maybe a one-liner for range:
Code:
for r in $(for i; do [[ "$i" =~ "-" ]] && echo -n "$(seq -s" " ${i/-/ 1 }) " || echo -n "$i "; done)
   do ...

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Should pick latest file within past 3 days using UNIX script and perform steps in message below.

Hi , Can anyone help me how do perform below requirement in unix. Step1:we will receive multiple files weekly with same name(as below) in a folder(In folder we will have other files also def.dat,ghf.dat) Filenames: 1) abc_20171204_052389.dat 2)abc_20171204_052428.dat DON'T modify... (23 Replies)
Discussion started by: sunnykamal59
23 Replies

2. Shell Programming and Scripting

Problems passing shell arguments to perl

Semi-newbie, so flame throwers to 'singe-only', please. ;-) I have a large number of (say) .html files, where I'd like to do a recursive in-place search and replace a particular string. The following bit of perl works fine: perl -pi -e 's/oldstring/newstring/g' `find ./ -name *.html` ... (2 Replies)
Discussion started by: johnny_canucl
2 Replies

3. Shell Programming and Scripting

Script variable help, Varying number of arguments to excute script

Hi Guy's. Hopefully someone can help me with what I am trying to archieve. So situation currently is, I have a script already setup however I have another script that sits infront of it. The main script basically goes and searchs multiple platforms for a list of entered data. In... (10 Replies)
Discussion started by: mutley2202
10 Replies

4. Shell Programming and Scripting

Bash script with arguments

Could someone help me with the script below? I am trying to make a script having just one arguement as a command and then it executes the appropriate code #!/bin/bash if then echo "Available commands:" echo "./exec.sh cmd1" echo "./exec.sh cmd2" elif then cmd1 =... (1 Reply)
Discussion started by: spiridakos
1 Replies

5. Shell Programming and Scripting

sub arguments to shell script

Hi, I have a shell script, when run it i get a prompt to enter arguments say 1 for doing my next task otherwise q for quit. What I am trying to do is run the shell script with the argument passed in however it does not seem to work. This is what I did ./test.sh 1 Instead it printed the line... (6 Replies)
Discussion started by: aqua9
6 Replies

6. Shell Programming and Scripting

Using arguments in Shell script

Hello, I have to make a shell script doing that : the program tests if there is an argument, if there is it checks whether this is a directory or not, If it is it opens it. for any .c file in the directory it prints 2 lines in the screen : the dependence line of the .o and compiler commend... (1 Reply)
Discussion started by: dekl
1 Replies

7. Shell Programming and Scripting

need help to pass arguments in script

Hi, I have a my script here-- print "The Perl Script does the User health check and system health check...\n"; print "---------------------------------------------------------------------\n"; # use strict; my($OS); $OS = $^O; # need to test @ARGV before GetOptions shifts it if (@ARGV... (1 Reply)
Discussion started by: namishtiwari
1 Replies

8. Shell Programming and Scripting

Help with options and arguments to a script

I'm trying to write a script that accepts both arguments and options, e.g. ./script -h 1 -m 15 -s 30 or ./script -h 1 -m 15 -s 30 I'd like for any of the arguments and options to be optional, and the option values should be numerals only. I've tried both getopt and getopts but I... (1 Reply)
Discussion started by: Ilja
1 Replies

9. Shell Programming and Scripting

use several inputs as arguments in my script

Hi there, It's pretty hard for me to explain my problem because I'm affraid I'm not using the correct vocabulary. So let me describe the situation. I wrote a script that has one argument. It works like this: ~$ cat /usr/local/bin/squote echo "$@" | sed 's/'\''/'\''\\'\'\''/g; s/.*/'\''&'\''/g'... (2 Replies)
Discussion started by: chebarbudo
2 Replies

10. Shell Programming and Scripting

Passing arguments to a script

I've written a script (bgrep) for a more advanced grep command (& attached a cut down version below). I'm trying allow all grep options to be used, or in any combination. The script works fine if I type say bgrep -i -files product it will return a non-case sensitive list of matches for... (3 Replies)
Discussion started by: Kevin Pryke
3 Replies
Login or Register to Ask a Question