david-e
david-e
Reputation Top 5%
Davide Viti
2 Snippets  (222nd place)
Published
0 Channels
Created
0 Channels
Following
48 points  (632nd place)
Reputation
Junior Code Generator
Junior Publisher
Junior Popular Coder
Junior Autobiographer
Serious Autobiographer
Senior Autobiographer
Junior Famous Coder

Recent Snippets See all snippets by david-e

public by david-e  2868  15  5  0

Bash getops example

Simple example of argument parsing with getopts
#!/bin/bash                                                                                                 
usage()
{   
    echo ""
    echo "$0 -f <csv|html> -i <IP address> -l"
    echo ""
    echo "Example:"
    echo " $0 -f html -i 172.16.88.83"
    echo " $0 -f csv  -i 172.16.88.83 -l"
}

#default value                                                                                              
FORMAT="csv"
IPADDR=""
LOAD=false
while getopts ":f:i:l" optname
do  
    case "$optname" in
        "f")
            if [[ ${OPTARG} == "csv" || ${OPTARG} == "html" ]] ; then
                FORMAT="${OPTARG}"
            else
                echo "Output format not supported"
                usage
                exit 1
            fi
            ;;
        "i")
            IPADDR="${OPTARG}"
            ;;
        "l")
            LOAD=true
            ;;
        "?")
            usage
            exit 1
            ;;
        ":")
            echo "No argument value for option $OPTARG"
            ;;
        *)
            echo "Unknown error while processing options"
            ;;
    esac
done

# Mandatory arguments                                                                                       
if [[ -z ${FORMAT} ]] || [[ -z ${IPADDR} ]]
then
    usage
    exit 1
fi

echo "format: $FORMAT"
echo "ipaddr: $IPADDR"
if [[ ${LOAD} == "true" ]] ; then
    echo "load: true"
else
    echo "load: false"
fi
;

public by david-e  2232  1  6  0

Perl one-liner to print repeated characters

Simple one-liner I've used in the past to print repeated characters. It outputs something like this: 00000... 11111... ... 99999...
perl -e 'for my $i (0..9){print "$i" x 80; print "\n";}'
;