Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am not sure if I performed the following illegal or true

but what I need is - to create set of ready commands
so if I need to use , for example to match IP address with 4 octet I can use the command - command_that_match_four_octet

Please advice if I performed correctly the following and, I want to know if the following syntax will not cause troubles.

[root@su1a /tmp]#  command_that_match_four_octet=" grep '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' "

[root@su1a /tmp]# command_that_match_three_three_octet=" grep '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' "


[root@su1a /tmp]# echo  | eval $command_that_match_four_octet

[root@su1a /tmp]# echo 23.4.5 | eval $command_that_match_three_three_octet
share|improve this question
Maybe a question for – F. Hauri Dec 30 '12 at 20:05
Shot answer is Yes, but no. This may work, but using eval is not a good practice. – F. Hauri Dec 30 '12 at 21:26
up vote 1 down vote accepted

what you call shortcut seem to be a kind of alias or more complex functions.

for answering you ask, you could:

alias checkCommand="grep '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'"
echo  | checkCommand


function checkIsIp() {
    grep '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'
echo  | checkIsIp

There is a bash function that will check for IP (v4) and more this will compute 32bit integer if argument is a valid IP and return a RC > 0 if else:

function check_Is_Ip() {
    local IFS=.
    set -- $1
    [ $# -eq 4 ] || return 2
    local var
    for var in $* ;do
        [ $var -lt 0 ] || [ $var -gt 255 ] && return 3
    echo $(( ($1<<24) + ($2<<16) + ($3<<8) + $4))

than now:

if check_Is_Ip >/dev/null; then echo It is. ;else echo There is not. ;fi
It is.

if check_Is_Ip >/dev/null; then echo It is. ;else echo There is not. ;fi
It is.

if check_Is_Ip >/dev/null; then echo It is. ;else echo There is not. ;fi
There is not.

and useable for IP calculation:

There is the back function:

int2ip() {
    echo $(($1>>24)).$(($1>>16&255)).$(($1>>8&255)).$(($1&255))


int2ip $((4294967040 & 3232235807))

So as a good practice, you could:

function die() {
    echo "Error: $@" >&2
    exit 1


intIp=$(check_Is_Ip $netIp) || die "Submited IP: '$netIP' is not an IPv4 address."
intMask=$(check_Is_Ip $netMask) || die "Submited Mask: '$netMask' not IPv4."
netBase=$(int2ip $(( intIp & intMask )) )
netBcst=$(int2ip $(( intIp | intMask ^ ( (1<<32) - 1 ) )) )
printf "%-20s: %s\n" \
    Address $netIp Netmask $netMask Network $netBase Broadcast $netBcst

Address             :
Netmask             :
Network             :
Broadcast           :

Where check, validation and conversion of inputs are done is only one operation:

intMask=$(check_Is_Ip $netMask) || die "Submited Mask: '$netMask' not IPv4."

If $netMask don't match IPv4, the command check_Is_Ip will fail, then die will be executed. If else, result of conversion will be stored in intMask variable.

share|improve this answer

If you want to create a shortcut to another command, you can use the builtin shell command alias. For more info, you can see man bash.

As an example, you can use:

$ alias your_command_name='your complicated and long command'
share|improve this answer
I need only to know if can use the commands as I write in my question , I only want to understand if they are correctly good enough – yael Dec 30 '12 at 12:37

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.