LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   add options to bash script (http://www.linuxquestions.org/questions/programming-9/add-options-to-bash-script-4175422336/)

pablgonz 08-16-2012 12:37 AM

add options to bash script
 
Hello forum friends, I have the following bash script (which works perfectly), the code is as follows:
Code:

#!/bin/bash
# Compile big TeX proyect
# Set work dir
DIR="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Take name for proyect and create dir (mandato...)
echo Enter Name of proyect
read NAME
mkdir -p $DIR/backup/$NAME/{book,screen,legal,booklet}
echo "El Arbol de directorios para $NAME ha sido creado"
# First Option
# Compiling book (2 run)
for file in $DIR/source/book/*.tex
do pdflatex -interaction=batchmode -output-directory=$DIR/backup/$NAME/book/ "$file" >/dev/null 2>&1 &&
  pdflatex -interaction=batchmode -output-directory=$DIR/backup/$NAME/book/ "$file" >/dev/null 2>&1
done
echo "El Libro se ha creado con exito!"
# Second Option
# Compiling screen
for file in $DIR/source/screen/*.tex
do pdflatex -interaction=batchmode -output-directory=$DIR/backup/$NAME/screen/ "$file" >/dev/null 2>&1
done
echo "Las Presentaciones se han creado con exito!"
# Third Option
# Compilando legal
for file in $DIR/source/legal/*.tex
do pdflatex -interaction=batchmode -output-directory=$DIR/backup/$NAME/legal/ "$file" >/dev/null 2>&1
done
echo "El formato LEGAL se ha creado con exito!"
# Fourth Option
cp -R $DIR/source/facsimil/ /$DIR/backup/$NAME/
for file in $DIR/source/booklet/*.tex
do pdflatex -interaction=batchmode -output-directory=$DIR/backup/$NAME/facsimil "$file" >/dev/null 2>&1
done
cd $DIR/backup/$NAME/facsimil &&
for file in *.tex
do pdflatex -interaction=batchmode -output-directory=$DIR/backup/$NAME/booklet/ "$file" >/dev/null 2>&1
done
# Clean temp files and facsimil dir
rm -rf $DIR/backup/$NAME/facsimil/ &&
find $DIR/backup/$NAME/ -regextype posix-awk -regex "(.*.log|.*.toc|.*.xwm|.*.out|.*.aux)" -exec rm -rf {} \; &&
cd $DIR/backup/ &&
# Create back folder
tar -c --xz -f $NAME-$(date +%Y%m%d).tar.xz $NAME/
echo "El respaldo $NAME.tar.xz se ha creado con exito!"
# Move proyect
mv $DIR/backup/$NAME/ $DIR/store/
echo "The $NAME proyect its done"
exit 0

but, I would like to run it in parts, that is, add options to run.
My idea is to add the following
- book (create book)
- booklet (create booklet)
- screen (create screen)
- legal (make legal)
- all (all formats)
try to get married, but did not get what he wanted (I should clarify that I am not a programmer, and I believe it from google, this and other forums Linux users) if you help me with some example, or tell me I should improve on this script, would appreciate it.
regards
This its a second version (using case)
Code:

#!/bin/bash
# Compile big TeX proyect
# Set work dir
DIR="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo Ingrese el nombre del proyecto
read NAME
echo 'Ingresa los parámetros'
read par
case $par in
        -b|book)
                mkdir -p $DIR/backup/$NAME/book &&
                echo "Creando libro para $NAME"
                for file in $DIR/source/book/*.tex
                do pdflatex -interaction=batchmode -output-directory=$DIR/backup/$NAME/book/ "$file" >/dev/null 2>&1 &&
                  pdflatex -interaction=batchmode -output-directory=$DIR/backup/$NAME/book/ "$file" >/dev/null 2>&1
                done
                echo "Done!"
                ;;
        -l|legal)
                mkdir -p $DIR/backup/$NAME/legal &&
                for file in $DIR/source/legal/*.tex
                do pdflatex -interaction=batchmode -output-directory=$DIR/backup/$NAME/legal/ "$file" >/dev/null 2>&1
                done
                echo "Done!"
                ;;
        -f|facsimil)
                mkdir -p $DIR/backup/$NAME/booklet
                cp -R $DIR/source/facsimil/ /$DIR/backup/$NAME/ &&
                for file in $DIR/source/booklet/*.tex
                do pdflatex -interaction=batchmode -output-directory=$DIR/backup/$NAME/facsimil "$file" >/dev/null 2>&1
                done &&
                cd $DIR/backup/$NAME/facsimil &&
                for file in *.tex
                do pdflatex -interaction=batchmode -output-directory=$DIR/backup/$NAME/booklet/ "$file" >/dev/null 2>&1
                done &&
                rm -rf $DIR/backup/$NAME/facsimil/
                ;;
        -s|screen)
                mkdir -p $DIR/backup/$NAME/screen &&
                for file in $DIR/source/screen/*.tex
                do pdflatex -interaction=batchmode -output-directory=$DIR/backup/$NAME/screen/ "$file" >/dev/null 2>&1
                done
                echo "Done!"
                ;;

        *)
                echo "The big TeX Proyect"
                echo "Usage: $0 {book|legal|facsimil|screen}"
                echo "        book    : Crea libro con marcas de agua."
                echo "        legal    : Crea las guías en formato legal."       
                echo "        screen  : Crea las presentaciones en pdf."
                echo "        facsimil : Crea los facsímiles en pdf."       
        ;;
esac
# Clean temp files
find $DIR/backup/$NAME/ -regextype posix-awk -regex "(.*.log|.*.toc|.*.xwm|.*.out|.*.aux)" -exec rm -rf {} \; &&
cd $DIR/backup/ &&
# Create backup folder
tar -c --xz -f $NAME-$(date +%Y%m%d).tar.xz $NAME/
echo "El respaldo del proyecto $NAME ha creado con exito!"
# Move proyect
mv $DIR/backup/$NAME/ $DIR/store/
echo "FINISH"
exit 0

but stay trapped to give more than one option to the script (-b -f -s -l and create the option - all)
Pablo

David the H. 08-16-2012 08:29 AM

Please don't double-post your requests. Keep everything to one thread. I just finished replying to your other one here:

http://www.linuxquestions.org/questi...pt-4175422321/

I'm reporting this to the mods so they can decide what to do, considering that this thread has additional info. They may have to merge them in some way.

pablgonz 08-16-2012 09:05 AM

apologies, I did not realize that it has doubled the post (I thought I had edited), do not know how to delete ...

David the H. 08-16-2012 09:52 AM

Any time you have trouble with your post (double posting, wrong forum), just hit the "report" button at the bottom of it, and ask the moderators to fix it.

Mara 08-17-2012 03:28 PM

Please post all replies to http://www.linuxquestions.org/questi...pt-4175422321/

This thread is closed.

Mara 08-17-2012 03:29 PM

Please post all replies to http://www.linuxquestions.org/questi...pt-4175422321/

This thread is closed


All times are GMT -5. The time now is 12:39 AM.