Results 1 to 1 of 1
hi to everyone
i am new to linux and bash and i am trying to build a bash script, that is quite similar to the well known cmd 'split' ...
...
- 04-13-2011 #1Just Joined!
- Join Date
- Apr 2011
- Posts
- 1
help with a bash script
hi to everyone

i am new to linux and bash and i am trying to build a bash script, that is quite similar to the well known cmd 'split' ...
it is now already "working" ... i can use it like:
./splitfix.sh -v -s 10 foo
./splitfix.sh -s 10 -v foo
./splitfix.sh -s 10 foo
./splitfix.sh -v foo
when i try wrong params i dont get an error (or the shorthelp) function and i dont know how to implement the --verbose tag in my code
this is the third version of my script and i cant find a possibility to check the parameters.
i always have the problem, that if one parameter is -s that than has to follow a number (size in bytes or lines).
can anybody help me out?
Code:#!/bin/bash #simple Tool to split files and txt files #02.04.2011 ########################################### function usage () { cat << EOF $0 [OPTIONS] FILE [FILE ...] Split FILE into fixed-size pieces. The pieces are 10 lines long, if FILE is a text file. The pieces are 10 kiB long, if FILE is *not* a text file. The last piece may be smaller, it contains the rest of the original file. The output files bear the name of the input file with a 4-digit numerical suffix. The original file can be reconstructed with the command ``cat FILE.*´´ EXAMPLE: splitfix.sh foo.pdf splits foo.pdf into the files foo.pdf.0000 foo.pdf.0001 etc. splitfix.sh [-h | --help] This help text splitfix.sh --version Print version number OPTIONS: -h --help this help text -s SIZE size of the pieces in lines (for text files) or in bytes (for other files) -v --verbose print debugging messages EOF } ########################################### function shorthelp () { cat << EOF Usage: splitfix.sh [OPTIONS] FILE [FILE ...] »splitfix-sh --help« for more information. EOF } ########################################### function check_param () { if [[ $1 == '-s' ]] then printf "%d" $2 > /dev/null 2>&1 if [[ $? == 1 ]] then shorthelp exit 1 else SIZE=$2 fi if [[ $3 == '-v' ]] then shift 3 verbose=on splitting $SIZE $verbose $* else shift 2 echo "debug2" splitting $SIZE $* fi else if [[ $1 == '-v' ]] then shift 1 verbose=on if [[ $1 == '-s' ]] then printf "%d" $2 > /dev/null 2>&1 if [[ $? == 1 ]] then shorthelp exit 1 else SIZE=$2 shift 2 splitting $SIZE $verbose $* fi else splitting $verbose $* fi fi fi } ################################################ function splitting () { if [[ $SIZE != "" ]] then shift 1 fi if [[ $verbose == "on" ]] then shift 1 fi for i in $* do # Existiert die Datei? if [[ -e $i ]] then file --mime-type $i | grep -iq text if [ $? -eq 0 ] then if [[ $SIZE -gt 0 ]] then if [[ $verbose = "on" ]] then echo "Die Textdatei wird gesplited" fi split --suffix-length=4 -d --lines=$SIZE $i $i. else if [[ $verbose = "on" ]] then echo "Die Textdatei wird gesplited" fi split --suffix-length=4 -d --lines=10 $i $i. fi else if [[ $SIZE -gt 0 ]] then if [[ $verbose = "on" ]] then echo "Die Binärdatei wird gesplited" fi split --suffix-length=4 -d --bytes=$SIZE $i $i. else if [[ $verbose = "on" ]] then echo "Die Binärdatei wird gesplited" fi split --suffix-length=4 -d --bytes=10000 $i $i. fi fi else echo "File $i does not exist." exit 1 fi done } ########################################### # main() case "$1" in '--help' | '-h') usage ;; *) # Mindestens ein Argument.. if [ $# -gt 0 ] then check_param $* exit 0 else shorthelp exit 1 fi esac


Reply With Quote