0

I have two scripts, one that is used on a folder with csv files and another on a folder with .tar.gz files. In the second script, I first unzip the files before doing anything with them. Now I want to combine both scripts into one script where a user can specify which folder he/she wants to work with. When the script is run, the user has two options, to either use folderA or folderB. Sort of like a menu.

ScriptA

#!/bin/bash
FOLDER=/path/to/folder
DATE_LOG=`date "+%Y-%m-%d-%H:%M:%S"`
LOG_FILE=/home/kamil/Desktop/Script/log_$DATE_LOG.txt


# Getting the pattern and headers of files from FOLDER
cd "$FOLDER"
    for file in *.csv; do
    echo -n "${file}" "|" | sed -r 's/(.*)_[0-9]{8}_[0-9][0-9]-[0-9][0-9].[0-9][0-9].csv/\1/'
        head -1 "$file"
    done | tee $LOG_FILE

ScriptB

#!/bin/bash
FOLDER_HISTO=/path/to/folder/WithTAR.GZ
DATE_LOG=`date "+%Y-%m-%d-%H:%M:%S"`
LOG_FILE=/home/kamil/Desktop/Script/log_$DATE_LOG.txt


# Getting the pattern and headers of files from FOLDER_HISTO
cd "$FOLDER_HISTO"
    for zip_file in *.tar.gz; do
    file=`tar -xvf $zip_file`
    echo -n "${file}" "|" | sed -r 's/(.*)_[0-9]{8}_[0-9][0-9]-[0-9][0-9].[0-9][0-9].csv/\1/'
        head -1 "$file"
    done | tee $LOG_FILE 
  • The second script assigns the variable FOLDER_HISTO, but then uses $FOLDER. Is that a copying error? – Barmar Dec 30 '14 at 22:00
  • It also uses a variable $FICHIER_LOG that wasn't set. – Barmar Dec 30 '14 at 22:00
  • Yes copying error – pathy Dec 30 '14 at 22:02
1

I suppose the standard CLI way to make a menu is select:

select response in "Folder 1" "Folder 2"
do
    case $response in
        "Folder 1") cd folder1; /path/to/ScriptA.sh; exit ;;
        "Folder 2") cd folder2; /path/to/ScriptB.sh; exit ;;
    esac
done
  • Yes, but i want only one script not two separate scripts. – pathy Dec 30 '14 at 22:03
  • @musty So? Replace /path/to/ScriptA.sh with the commands in the first script and /path/to/ScriptB.sh with the commands in the second script. – muru Dec 30 '14 at 22:05
1

The scripts are so similar, I would combine them into one script. It would take two arguments: an option -tar or -csv to indicate how the files should be expanded, and a directory name for where to cd to.

MODE=$1
FOLDER=$2
DATE_LOG=`date "+%Y-%m-%d-%H:%M:%S"`
LOG_FILE=/home/kamil/Desktop/Script/log_$DATE_LOG.txt

case "$mode" in 
    -tar) SUFFIX=tar.gz
          CMD="tar -xvf" 
        ;;
    -csv) SUFFIX=csv
          CMD="cat" 
        ;;
    *) echo Invalid mode "$mode" >&2; exit 1 ;;
esac

if ! cd "$FOLDER"
then echo "Bad folder $FOLDER" >&2
     exit 1
fi
for file in *.SUFFIX; do
    contents=`$CMD $file`
    echo -n "$contents" "|" | sed -r 's/(.*)_[0-9]{8}_[0-9][0-9]-[0-9][0-9].[0-9][0-9].csv/\1/'
    head -1 "$contents"
done | tee $LOG_FILE

The stuff you're doing in the for loop doesn't look right, I suspect that's more copying errors. Hopefully you'll be able to figure out how to fit what you're actually doing into the above structure. The general idea is to find all the places where the two scripts differ, and put them into variables that are set in the case statement. You might not be able to do everything with simple variable substitutions like I did -- you might need an if statement as well.

This is called the DRY principle.

If the folders are always the same depending on the mode, you could put the FOLDER assignment into the case statement, instead of requiring it on the command line. You could also replace the command-line option with a menu as in muru's answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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