diff --git a/README.md b/README.md index 4c0efbb226d5b96a724c4abd7cc70666b02e9ac2..1bc63411c88b50b3d6a3865aaa8856c1b082000d 100644 --- a/README.md +++ b/README.md @@ -1,76 +1,82 @@ - -#### DESCRIPTION - -Little bash Functions contains the 9 following functions: -show_time_fun -test_tab_in_file_fun -test_space_in_file_fun -space_replacement_in_file_fun -file_pattern_detection_fun -test_column_nb_perline_in_file_fun -single_path_with_regex_fun -var_existence_check_fun -file_exten_verif_name_recov_fun - - -#### LICENCE - -This package of scripts can be redistributed and/or modified under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. -Distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -See the GNU General Public License for more details at https://www.gnu.org/licenses. - - -#### CREDITS - -Gael A. Millot, Hub-C3BI, Institut Pasteur, USR 3756 IP CNRS, Paris, France - - -#### HOW TO USE IT - -Download the desired Tagged version, never the current master. - -To directly source a version in the environment: -source <(curl -s https://gitlab.pasteur.fr/gmillot/little_bash_functions/raw/v1.0.0/little_bash_functions.sh) - -To directly save the little_bash_functions.sh file: -wget https://gitlab.pasteur.fr/gmillot/little_bash_functions/raw/v1.0.0/little_bash_functions.sh - -To save all the files: -wget https://gitlab.pasteur.fr/gmillot/little_bash_functions/-/archive/v1.0.0/little_bash_functions-v1.0.0.zip - -To source the local little_bash_functions.sh to have the functions available in the working environment: -source /pasteur/homes/gmillot/Git_versions_to_use/little_bash_functions-v1.0.0/little_bash_functions.sh - -Description of the functions is at the beginning of the function body. To obtain it, read the little_bash_functions.sh - - -#### FILE DESCRIPTIONS - -little_bash_functions.sh file that has to be sourced - - -#### WEB LOCATION - -Check for updated versions (more recent release tags) at https://gitlab.pasteur.fr/gmillot/little_bash_functions/tags - - -#### WHAT'S NEW IN - -## v1.3.0 - -test_column_nb_perline_in_file_fun with better writting - - -## v1.2.0 - -output file name error in test_column_nb_perline_in_file_fun fixed - - -## v1.1.0 - -single_path_with_regex_fun function deals now with url - - -## v1.0.0 - -Everything + +#### DESCRIPTION + +Little bash Functions contains the 9 following functions: +show_time_fun +test_tab_in_file_fun +test_space_in_file_fun +space_replacement_in_file_fun +file_pattern_detection_fun +test_column_nb_perline_in_file_fun +single_path_with_regex_fun +var_existence_check_fun +file_exten_verif_name_recov_fun + + +#### LICENCE + +This package of scripts can be redistributed and/or modified under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. +Distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +See the GNU General Public License for more details at https://www.gnu.org/licenses. + + +#### CREDITS + +Gael A. Millot, Hub-C3BI, Institut Pasteur, USR 3756 IP CNRS, Paris, France + + +#### HOW TO USE IT + +Download the desired Tagged version, never the current master. + +To directly source a version in the environment: +source <(curl -s https://gitlab.pasteur.fr/gmillot/little_bash_functions/raw/v1.0.0/little_bash_functions.sh) + +To directly save the little_bash_functions.sh file: +wget https://gitlab.pasteur.fr/gmillot/little_bash_functions/raw/v1.0.0/little_bash_functions.sh + +To save all the files: +wget https://gitlab.pasteur.fr/gmillot/little_bash_functions/-/archive/v1.0.0/little_bash_functions-v1.0.0.zip + +To source the local little_bash_functions.sh to have the functions available in the working environment: +source /pasteur/homes/gmillot/Git_versions_to_use/little_bash_functions-v1.0.0/little_bash_functions.sh + +Description of the functions is at the beginning of the function body. To obtain it, read the little_bash_functions.sh + + +#### FILE DESCRIPTIONS + +little_bash_functions.sh file that has to be sourced + + +#### WEB LOCATION + +Check for updated versions (more recent release tags) at https://gitlab.pasteur.fr/gmillot/little_bash_functions/tags + + +#### WHAT'S NEW IN + + +## v2.0 + +function modified because of return. echo $? cannot be used with. Use global variables as explained inside functions + + +## v1.3.0 + +test_column_nb_perline_in_file_fun with better writting + + +## v1.2.0 + +output file name error in test_column_nb_perline_in_file_fun fixed + + +## v1.1.0 + +single_path_with_regex_fun function deals now with url + + +## v1.0.0 + +Everything diff --git a/little_bash_functions.sh b/little_bash_functions.sh index 06a48195e00aeee5fc2a66c33c24c79c87b22b78..5de3ffe3e47ba9e767cd06844ecf3d4a45ce20f3 100644 --- a/little_bash_functions.sh +++ b/little_bash_functions.sh @@ -1,8 +1,8 @@ -#!/bin/bash # shebang +#!/bin/bash -uel # shebang ################################################################ ## ## -## LITTLE BASH FUNCTIONS v1.3.0 ## +## LITTLE BASH FUNCTIONS v2.0 ## ## ## ## Gael A. Millot ## ## ## @@ -63,32 +63,38 @@ function show_time_fun () { function test_tab_in_file_fun () { # DESCRIPTION # test if tabulations are present in a file + # WARNING + # I had to use a global variable file_pattern_detection_fun_RETURN and not a local one to be able to deal with the check.sh file in genetic_linkage project # ARGUMENTS # $0: name of the function # $1: first argument, ie file name and path # RETURN OUTPUT - # 0: tabulation detected - # 1: error in $1 - # 2: no tabulation detected + # test_tab_in_file_fun_RETURN=0: tabulation detected + # test_tab_in_file_fun_RETURN=1: error in $1 + # test_tab_in_file_fun_RETURN=2: no tabulation detected # EXAMPLES # test_tab_in_file_fun /cygdrive/c/Users/Gael/Desktop/Archive_Guillaume/Group1/freq # test_tab_in_file_fun /cygdrive/c/Users/Gael/Desktop/Archive_Guillaume/Group1/pedfile1.pro # argument checking if [[ -z $1 ]]; then echo -e "\n### test_tab_in_file_fun ERROR ### ARGUMENT 1 REQUIRED\n" - return 1 + test_tab_in_file_fun_RETURN=1 + return 0 fi if [[ ! -f $1 ]]; then echo -e "\n### test_tab_in_file_fun ERROR ### $1 FILE NOT PRESENT IN `dirname $1`\n" - return 1 + test_tab_in_file_fun_RETURN=1 + return 0 fi # main code local TEMPO_TAB_COUNT=$(grep -o $'\t' $1 | wc -l) # for tabulation problem in bash and grep, see https://askubuntu.com/questions/53071/how-to-grep-for-tabs-without-using-literal-tabs-and-why-does-t-not-work if [[ $TEMPO_TAB_COUNT == 0 ]] ; then echo -e "\n### test_tab_in_file_fun ERROR: NO TABULATIONS DETECTED IN $1\n" - return 2 + test_tab_in_file_fun_RETURN=2 + return 0 else echo -e "\ntest_tab_in_file_fun MESSAGE: TABULATIONS DETECTED IN $1 AS EXPECTED\n" + test_tab_in_file_fun_RETURN=0 return 0 fi } @@ -98,32 +104,38 @@ function test_tab_in_file_fun () { function test_space_in_file_fun () { # DESCRIPTION # test if spaces are present in a file + # WARNING + # I had to use a global variable file_pattern_detection_fun_RETURN and not a local one to be able to deal with the check.sh file in genetic_linkage project # ARGUMENTS # $0: name of the function # $1: first argument, ie file name and path # RETURN OUTPUT - # 0: space detected - # 1: error in $1 - # 2: no space detected + # test_space_in_file_fun_RETURN=0: space detected + # test_space_in_file_fun_RETURN=1: error in $1 + # test_space_in_file_fun_RETURN=2: no space detected # EXAMPLES # test_space_in_file_fun /cygdrive/c/Users/Gael/Desktop/Archive_Guillaume/Group1/freq # test_space_in_file_fun /cygdrive/c/Users/Gael/Desktop/Archive_Guillaume/Group1/pedfile1.pro # argument checking if [[ -z $1 ]]; then echo -e "\n### test_space_in_file_fun ERROR ### ARGUMENT 1 REQUIRED\n" - return 1 + test_space_in_file_fun_RETURN=1 + return 0 fi if [[ ! -f $1 ]]; then echo -e "\n### test_space_in_file_fun ERROR ### $1 FILE NOT PRESENT IN `dirname $1`\n" - return 1 + test_space_in_file_fun_RETURN=1 + return 0 fi # main code local TEMPO_SPACE_COUNT=$(grep -o ' ' $1 | wc -l) if [[ $TEMPO_SPACE_COUNT == 0 ]] ; then echo -e "\ntest_space_in_file_fun MESSAGE: NO SPACE DETECTED IN $1\n" - return 2 + test_space_in_file_fun_RETURN=2 + return 0 else echo -e "\ntest_space_in_file_fun MESSAGE: ${TEMPO_SPACE_COUNT} SPACES DETECTED IN $1\n" + test_space_in_file_fun_RETURN=0 return 0 fi } @@ -133,6 +145,8 @@ function test_space_in_file_fun () { function space_replacement_in_file_fun () { # DESCRIPTION # replace each block of succesive spaces by a single character string (default: _._) and save in -o (by default $1_replace) + # WARNING + # I had to use a global variable file_pattern_detection_fun_RETURN and not a local one to be able to deal with the check.sh file in genetic_linkage project # ARGUMENTS # $0: name of the function # -i or --input: file name and path @@ -140,9 +154,9 @@ function space_replacement_in_file_fun () { # IF -o not specified, the file name is -i_replace and must not already exists in -i location # -r or --replace: character string replacement (default "_._" if not specified) # RETURN OUTPUT - # 0: -i saved in -o then spaces replaced in -o - # 1: error in options - # 2: no space detected + # space_replacement_in_file_fun_RETURN=0: -i saved in -o then spaces replaced in -o + # space_replacement_in_file_fun_RETURN=1: error in options + # space_replacement_in_file_fun_RETURN=2: no space detected # EXAMPLES # space_replacement_in_file_fun -i /cygdrive/c/Users/Gael/Desktop/Archive_Guillaume/Group1/freq # space_replacement_in_file_fun -i /cygdrive/c/Users/Gael/Desktop/Archive_Guillaume/Group1/Group1.txt @@ -170,17 +184,20 @@ function space_replacement_in_file_fun () { if [[ -z $INPUT_FUN ]]; then echo -e "\n### space_replacement_in_file_fun ERROR ### OPTION -i REQUIRED\n" - return 1 + space_replacement_in_file_fun_RETURN=1 + return 0 elif [[ ! -f $INPUT_FUN ]]; then echo -e "\n### space_replacement_in_file_fun ERROR ### ${INPUT_FUN} FILE NOT PRESENT IN `dirname $INPUT_FUN`\n" - return 1 + space_replacement_in_file_fun_RETURN=1 + return 0 fi if [[ -z $OUTPUT_FUN ]]; then local OUTPUT_FUN=${INPUT_FUN}_replace fi if [[ -f $OUTPUT_FUN ]]; then echo -e "\n### space_replacement_in_file_fun ERROR ### ${OUTPUT_FUN} FILE ALREADY EXISTS\n" - return 1 + space_replacement_in_file_fun_RETURN=1 + return 0 fi if [[ -z $REPLACE_FUN ]]; then local REPLACE_FUN="_._" @@ -189,11 +206,13 @@ function space_replacement_in_file_fun () { local TEMPO_SPACE_COUNT=$(grep -o ' \+' $INPUT_FUN | wc -l) # \ \+ from 1 to n spaces. Beware: \ * is from 0 to n if [[ $TEMPO_SPACE_COUNT == 0 ]] ; then echo -e "\nspace_replacement_in_file_fun MESSAGE: NO SPACE REPLACED SINCE NO SPACE DETECTED IN ${INPUT_FUN}\n" - return 2 + space_replacement_in_file_fun_RETURN=2 + return 0 else echo -e "\nspace_replacement_in_file_fun MESSAGE: ${INPUT_FUN} SAVED IN ${OUTPUT_FUN} AND ${TEMPO_SPACE_COUNT} SPACES REPLACED BY ${REPLACE_FUN} IN ${OUTPUT_FUN}\n" cat $INPUT_FUN > $OUTPUT_FUN # save the initial file sed -i "s/\ \+/$REPLACE_FUN/g" $OUTPUT_FUN # space replacement by _._ in file + space_replacement_in_file_fun_RETURN=0 return 0 fi } @@ -203,6 +222,8 @@ function space_replacement_in_file_fun () { function file_pattern_detection_fun () { # DESCRIPTION # identify the lines of a file that contain a pattern + # WARNING + # I had to use a global variable file_pattern_detection_fun_RETURN and not a local one to be able to deal with the check.sh file in genetic_linkage project # ARGUMENTS # $0: name of the function # -i or --input: file name and path @@ -214,9 +235,9 @@ function file_pattern_detection_fun () { # IF -c not specified, the file name is -i_wo_pattern and must not already exists at -i location # -d or --detect: pattern (default "^#" if not specified, meaning detection of lines starting by #). Any regex between quotes # RETURN OUTPUT - # 0: at least one line contains the pattern -> saved in -o - # 1: error in options - # 2: no pattern detected + # file_pattern_detection_fun_RETURN=0: at least one line contains the pattern -> saved in -o + # file_pattern_detection_fun_RETURN=1: error in options + # file_pattern_detection_fun_RETURN=2: no pattern detected # EXAMPLES # file_pattern_detection_fun -i /cygdrive/c/Users/Gael/Desktop/Archive_Guillaume/Group1/pedfile1.pro -d "^#|^$" # line starting by #or empty line # argument checking @@ -225,7 +246,7 @@ function file_pattern_detection_fun () { ((OPTIND--)) # or ((OPTIND-1)) ? fi } - local OPTIND OPTION i o c d INPUT_FUN OUTPUT_FUN OUTPUT_WOPATTERN_FUN CHARACTER_FUN + local OPTIND OPTION i o c d INPUT_FUN OUTPUT_FUN OUTPUT_WOPATTERN_FUN CHARACTER_FUN RETURN while getopts ":i:o:c:d:" OPTION ; do # add : after the option name to specify that something is required (-h has nothing required after) # the first : before h is to induce getopts switching to "silent error reporting mode" (disable annoying messages). @@ -234,32 +255,36 @@ function file_pattern_detection_fun () { o) verif $OPTARG ; OUTPUT_FUN=$OPTARG ;; c) verif $OPTARG ; OUTPUT_WOPATTERN_FUN=$OPTARG ;; d) verif $OPTARG ; CHARACTER_FUN=$OPTARG ;; - \?) echo -e "\n### file_pattern_detection_fun ERROR ### INVALID OPTION\n" ; return 1 ;; - :) echo -e "\n### file_pattern_detection_fun ERROR ### OPTION -${OPTARG} REQUIRES AN ARGUMENT\n" ; return 1 ;; + \?) echo -e "\n### file_pattern_detection_fun ERROR ### INVALID OPTION\n" ; RETURN=1 ; return 0 ;; + :) echo -e "\n### file_pattern_detection_fun ERROR ### OPTION -${OPTARG} REQUIRES AN ARGUMENT\n" ; RETURN=1 ; return 0 ;; esac done shift $((OPTIND-1)) if [[ -z $INPUT_FUN ]]; then echo -e "\n### file_pattern_detection_fun ERROR ### OPTION -i REQUIRED\n" - return 1 + file_pattern_detection_fun_RETURN=1 + return 0 elif [[ ! -f $INPUT_FUN ]]; then echo -e "\n### file_pattern_detection_fun ERROR ### ${INPUT_FUN} FILE NOT PRESENT IN `dirname $INPUT_FUN`\n" - return 1 + file_pattern_detection_fun_RETURN=1 + return 0 fi if [[ -z $OUTPUT_FUN ]]; then local OUTPUT_FUN=${INPUT_FUN}_pattern fi if [[ -f $OUTPUT_FUN ]]; then echo -e "\n### file_pattern_detection_fun ERROR ### ${OUTPUT_FUN} FILE ALREADY EXISTS\n" - return 1 + file_pattern_detection_fun_RETURN=1 + return 0 fi if [[ -z $OUTPUT_WOPATTERN_FUN ]]; then local OUTPUT_WOPATTERN_FUN=${INPUT_FUN}_wo_pattern fi if [[ -f $OUTPUT_WOPATTERN_FUN ]]; then echo -e "\n### file_pattern_detection_fun ERROR ### ${OUTPUT_WOPATTERN_FUN} FILE ALREADY EXISTS\n" - return 1 + file_pattern_detection_fun_RETURN=1 + return 0 fi if [[ -z $CHARACTER_FUN ]]; then local CHARACTER_FUN="^#" @@ -272,9 +297,11 @@ function file_pattern_detection_fun () { rm $OUTPUT_FUN rm $OUTPUT_WOPATTERN_FUN echo -e "\nfile_pattern_detection_fun MESSAGE: NO $CHARACTER_FUN PATTERN DETECTED IN $INPUT_FUN\n" - return 2 + file_pattern_detection_fun_RETURN=2 + return 0 else echo -e "\nfile_pattern_detection_fun MESSAGE: PATTERN $CHARACTER_FUN DETECTED IN $INPUT_FUN\n" + file_pattern_detection_fun_RETURN=0 return 0 fi } @@ -284,10 +311,12 @@ function file_pattern_detection_fun () { function test_column_nb_perline_in_file_fun () { # DESCRIPTION # test if the number of column is identical for each line of a file + # WARNING + # I had to use a global variable file_pattern_detection_fun_RETURN and not a local one to be able to deal with the check.sh file in genetic_linkage project # ARGUMENTS # $0: name of the function # -i or --input: file name and path. Must be tab delimited - # -o or --output: output file in which problematic lines are printed. + # -o or --output: output file in which problematic lines are printed (lines with different number of columns than non empty first line). # If -o specified, the file must not exists. # IF -o not specified, by default, the file name is -i_error_line and must not already exists at -i location # -c or --cleanoutput: output file containing the lines without the problematic lines. @@ -295,11 +324,11 @@ function test_column_nb_perline_in_file_fun () { # IF -c not specified, the file name is -i_wo_error_line and must not already exists at -i location # -d or --detect: file separator pattern to count the number of columns (default "\t" if not specified). Any regex between quotes # RETURN OUTPUT - # 0: no difference in column number for each line of -i - # 1: error in options - # 2: error: empty first line - # 3: only the first line has a different column number from the rest of the $1 file - # 4: error lines (difference in column number in -i lines compared to 1st line of -i) -> printed in -o. First column is line number of -i. No error lines printed in -c + # test_column_nb_perline_in_file_fun_RETURN=0: no difference in column number for each line of -i + # test_column_nb_perline_in_file_fun_RETURN=1: error in options + # test_column_nb_perline_in_file_fun_RETURN=2: error: empty first line + # test_column_nb_perline_in_file_fun_RETURN=3: only the first line has a different column number from the rest of the $1 file + # test_column_nb_perline_in_file_fun_RETURN=4: error lines (difference in column number in -i lines compared to 1st line of -i) -> printed in -o. First column is line number of -i. No error lines printed in -c # EXAMPLE # test_column_nb_perline_in_file_fun -i /cygdrive/c/Users/Gael/Desktop/Archive_Guillaume/Group1/freq # test_column_nb_perline_in_file_fun -i /cygdrive/c/Users/Gael/Desktop/Archive_Guillaume/Group1/pedfile1.pro_wo_pattern @@ -326,24 +355,28 @@ function test_column_nb_perline_in_file_fun () { if [[ -z $INPUT_FUN ]]; then echo -e "\n### test_column_nb_perline_in_file_fun ERROR ### OPTION -i REQUIRED\n" - return 1 + test_column_nb_perline_in_file_fun_RETURN=1 + return 0 elif [[ ! -f $INPUT_FUN ]]; then echo -e "\n### test_column_nb_perline_in_file_fun ERROR ### ${INPUT_FUN} FILE NOT PRESENT IN `dirname $INPUT_FUN`\n" - return 1 + test_column_nb_perline_in_file_fun_RETURN=1 + return 0 fi if [[ -z $OUTPUT_ERROR_FUN ]]; then local OUTPUT_ERROR_FUN=${INPUT_FUN}_error_line fi if [[ -f $OUTPUT_ERROR_FUN ]]; then echo -e "\n### test_column_nb_perline_in_file_fun ERROR ### ${OUTPUT_ERROR_FUN} FILE ALREADY EXISTS\n" - return 1 + test_column_nb_perline_in_file_fun_RETURN=1 + return 0 fi if [[ -z $OUTPUT_WO_ERROR_FUN ]]; then local OUTPUT_WO_ERROR_FUN=${INPUT_FUN}_wo_error_line fi if [[ -f $OUTPUT_WO_ERROR_FUN ]]; then echo -e "\n### test_column_nb_perline_in_file_fun ERROR ### ${OUTPUT_WO_ERROR_FUN} FILE ALREADY EXISTS\n" - return 1 + test_column_nb_perline_in_file_fun_RETURN=1 + return 0 fi if [[ -z $CHARACTER_FUN ]]; then local CHARACTER_FUN="\t" @@ -362,7 +395,8 @@ function test_column_nb_perline_in_file_fun () { # echo -e "NB_COL_FIRSTLINE: $NB_COL_FIRSTLINE\n" if [[ -z $NB_COL_FIRSTLINE || $NB_COL_FIRSTLINE == 0 ]] ; then echo -e "\n### test_column_nb_perline_in_file_fun ERROR ### THE FIRST LINE OF $INPUT_FUN IS EMPTY: USE file_pattern_detection_fun TO REMOVE EMPTY LINES\n" - return 2 + test_column_nb_perline_in_file_fun_RETURN=2 + return 0 fi awk -F "$CHARACTER_FUN" -v var1=$NB_COL_FIRSTLINE 'NF!=var1{print NR, $0}' $INPUT_FUN > ${OUTPUT_ERROR_FUN} # recover the error lines awk -F "$CHARACTER_FUN" -v var1=$NB_COL_FIRSTLINE 'NF==var1{print $0}' $INPUT_FUN > $OUTPUT_WO_ERROR_FUN @@ -372,12 +406,14 @@ function test_column_nb_perline_in_file_fun () { echo -e "\ntest_column_nb_perline_in_file_fun MESSAGE: NO DIFFERENT COLUMN NUMBER FOUND IN LINES OF ${INPUT_FUN} HAVING $NB_COL_FIRSTLINE COLUMNS IN THE FIRST LINE\n" rm ${OUTPUT_ERROR_FUN} rm ${OUTPUT_WO_ERROR_FUN} + test_column_nb_perline_in_file_fun_RETURN=0 return 0 elif [[ $NB_ERROR_LINES == $(( ${TOTAL_NB_LINES} - 1 )) ]]; then echo -e "\ntest_column_nb_perline_in_file_fun MESSAGE: ONLY THE FIRST LINE HAS A DIFFERENT COLUMN NUMBER FROM THE REST OF THE ${INPUT_FUN} FILE\nTHE FIRST LINE IS:\n$(head -n 1 $INPUT_FUN)" rm ${OUTPUT_ERROR_FUN} rm ${OUTPUT_WO_ERROR_FUN} - return 3 + test_column_nb_perline_in_file_fun_RETURN=3 + return 0 else echo -e "test_column_nb_perline_in_file_fun MESSAGE: ${NB_ERROR_LINES} LINES FOUND IN ${INPUT_FUN} SHOWING A NUMBER OF COLUMN DIFFERENT FROM $NB_COL_FIRSTLINE COLUMNS AS FOUND IN THE FIRST LINE\n" # | tee -a $OUTPUT_ERROR_FUN # echo -e "THE PROBLEMATIC LINES ARE:\n" >> $OUTPUT_ERROR_FUN @@ -388,36 +424,40 @@ function test_column_nb_perline_in_file_fun () { else echo -e "\ntest_column_nb_perline_in_file_fun MESSAGE: THE PROBLEMATIC LINES ARE MORE THAN 200 -> ONLY PRINTED IN THE $OUTPUT_ERROR_FUN FILE:\n" fi - return 4 + test_column_nb_perline_in_file_fun_RETURN=4 + return 0 fi } function single_path_with_regex_fun { # comes from little_bash_functions-v1.0.0/little_bash_functions-v1.0.0.sh # DESCRIPTION # check that $1 path exists and is single - # BEWARE + # WARNING # do not use set -e ! + # I had to use a global variable file_pattern_detection_fun_RETURN and not a local one to be able to deal with the check.sh file in genetic_linkage project # ARGUMENTS # $0: name of the function # $1: first argument, ie path with regex # RETURN OUTPUT - # 0: single path detected is valid - # 1: error: $1 not provided - # 2: error: $2 provided or more than one path detected - # 3: single url detected does not exist - # 4: single path detected does not exist + # single_path_with_regex_fun_RETURN=0: single path detected is valid + # single_path_with_regex_fun_RETURN=1: error: $1 not provided + # single_path_with_regex_fun_RETURN=2: error: $2 provided or more than one path detected + # single_path_with_regex_fun_RETURN=3: single url detected does not exist + # single_path_with_regex_fun_RETURN=4: single path detected does not exist # EXAMPLES # single_path_with_regex_fun /cygdrive/c/Users/Gael/Desktop/config_tars_lodscore_gael_2017121[[:digit:]].conf # single_path_with_regex_fun /pasteur/homes/gmillot/dyslexia/code_gael/config_tars_lodscore_gael_2017121[[:digit:]].conf # argument checking if [[ -z $1 ]] ; then echo -e "\n### ERROR ### ARGUMENT 1 REQUIRED IN single_path_with_regex_fun\n" - return 1 + single_path_with_regex_fun_RETURN=1 + return 0 fi # main code if [[ ! -z $2 ]] ; then echo -e "\n### ERROR ###\n1) AT LEAST ARGUMENT 2 PRESENT IN single_path_with_regex_fun WHILE ONLY ONE REQUIRED OR\n2) REGEX PATH RESULT IN SEVERAL RESULTS:\n$@\n" - return 2 + single_path_with_regex_fun_RETURN=2 + return 0 fi local ARG1="$1" # echo $ARG1 # print ARG1 @@ -427,21 +467,25 @@ function single_path_with_regex_fun { # comes from little_bash_functions-v1.0.0/ # echo "LENGTH: $ARG1_ARR_LENGTH" # print the length of the array if [[ $ARG1_ARR_LENGTH != 0 ]] ; then echo -e "\n### ERROR ### SPECIFIED REGEXP PATH IN single_path_with_regex_fun CORRESPOND TO MORE THAN ONE PATH: ${ARG1_ARR_LENGTH[@]}\n"; - return 2 + single_path_with_regex_fun_RETURN=2 + return 0 else shopt -s extglob # -s unable global extention, ie the recognition of special global pattern in path, like [[:digit:]] if [[ $(echo ${ARG1_ARR[0]} | grep -cE '^http' ) == 1 ]] ; then # -cE to specify extended and -c to return the number of match (here 0 or one only) if [[ $(wget ${ARG1_ARR[0]} >/dev/null 2>&1 ; echo $?) != 0 ]] ; then # check the valid url. wget $url >/dev/null 2>&1 prevent any action and print. echo $? print the result of the last command (0 = success, other number = failure) echo -e "\n### ERROR ### SPECIFIED URL IN single_path_with_regex_fun DOES NOT EXISTS: ${ARG1_ARR[0]}\n"; shopt -u extglob # -u disable global extention, ie the recognition of special global pattern in path, like [[:digit:]] - return 3 + single_path_with_regex_fun_RETURN=3 + return 0 fi elif [[ ! ( -d ${ARG1_ARR[0]} || -f ${ARG1_ARR[0]} ) ]] ; then echo -e "\n### ERROR ### SPECIFIED PATH IN single_path_with_regex_fun DOES NOT EXISTS: ${ARG1_ARR[0]}\n"; shopt -u extglob # -u disable global extention, ie the recognition of special global pattern in path, like [[:digit:]] - return 4 + single_path_with_regex_fun_RETURN=4 + return 0 else shopt -u extglob # -u disable global extention, ie the recognition of special global pattern in path, like [[:digit:]] + single_path_with_regex_fun_RETURN=0 return 0 fi fi @@ -452,30 +496,35 @@ function var_existence_check_fun { # check that $1 variable exists # $0: name of the function # $1: first argument, ie variable name without $ - # BEWARE + # WARNING # do not use set -e ! + # I had to use a global variable file_pattern_detection_fun_RETURN and not a local one to be able to deal with the check.sh file in genetic_linkage project # RETURN OUTPUT - # 0: variable is detected in the environment - # 1: error: $1 not provided - # 2: error: $2 provided or more than one path detected - # 3: variable does not exist + # var_existence_check_fun_RETURN=0: variable is detected in the environment + # var_existence_check_fun_RETURN=1: error: $1 not provided + # var_existence_check_fun_RETURN=2: error: $2 provided or more than one path detected + # var_existence_check_fun_RETURN=3: variable does not exist # EXAMPLES # # argument checking if [[ -z $1 ]] ; then echo -e "\n### ERROR ### ARGUMENT 1 REQUIRED IN var_existence_check_fun\n" - return 1 + var_existence_check_fun_RETURN=1 + return 0 fi # main code if [[ ! -z $2 ]] ; then echo -e "\n### ERROR ###\n1) AT LEAST ARGUMENT 2 PRESENT IN var_existence_check_fun WHILE ONLY ONE REQUIRED OR\n2) REGEX PATH RESULT IN SEVERAL RESULTS:\n$@\n" - return 2 + var_existence_check_fun_RETURN=2 + return 0 fi ARG1=$(echo $1) ARG2=$(echo ${!ARG1}) # double because I need to know if the content of $(echo $1) is "" (echo return "" when does not exist) if [[ -z "$ARG2" ]] ; then # test if $ARG1 is "" - return 3 + var_existence_check_fun_RETURN=3 + return 0 else + var_existence_check_fun_RETURN=0 return 0 fi } @@ -485,14 +534,16 @@ function file_exten_verif_name_recov_fun () { # DESCRIPTION # check the file extension and recover the name of the file without extension # See https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html + # WARNING + # I had to use a global variable file_pattern_detection_fun_RETURN and not a local one to be able to deal with the check.sh file in genetic_linkage project # ARGUMENTS # $0: name of the function # -i or --input: file name, with or without the path # -e or --extensionpattern: character string of the extension, including dot (default ".txt" if not specified). Example: .txt # RETURN OUTPUT - # 0: pattern detected and name of the file without the extension printed - # 1: error in options - # 2: extension pattern not detected at the end of the file name + # file_exten_verif_name_recov_fun_RETURN=0: pattern detected and name of the file without the extension printed + # file_exten_verif_name_recov_fun_RETURN=1: error in options + # file_exten_verif_name_recov_fun_RETURN=2: extension pattern not detected at the end of the file name # EXAMPLES # file_exten_verif_name_recov_fun -i /cygdrive/c/Users/Gael/Desktop/Archive_Guillaume_split_initial_data/Famille1/pedfile.pro -e ".pro" # VAR1=$(file_exten_verif_name_recov_fun -i /cygdrive/c/Users/Gael/Desktop/Archive_Guillaume_split_initial_data/Famille1/pedfile.pro -e ".pro") ; echo $VAR1 @@ -517,7 +568,8 @@ function file_exten_verif_name_recov_fun () { if [[ -z $INPUT_FUN ]]; then echo -e "\n### file_exten_verif_name_recov_fun ERROR ### OPTION -i REQUIRED\n" - return 1 + file_exten_verif_name_recov_fun_RETURN=1 + return 0 fi if [[ -z $PATTERN_FUN ]]; then local PATTERN_FUN=".txt" @@ -528,10 +580,12 @@ function file_exten_verif_name_recov_fun () { local PATTERN_FUN_NCHAR=$(echo ${#PATTERN_FUN}) # recover the number of character of PATTERN_FUN if [[ ${FILENAME: -$PATTERN_FUN_NCHAR} != ${PATTERN_FUN} ]] ; then echo -e "\nfile_exten_verif_name_recov_fun MESSAGE: NO $PATTERN_FUN EXTENSION PATTERN DETECTED AT THE END OF $INPUT_FUN\n" - return 2 + file_exten_verif_name_recov_fun_RETURN=2 + return 0 else # do not write this because print output is assigned to a variable echo -e "\nfile_exten_verif_name_recov_fun MESSAGE: $PATTERN_FUN EXTENSION PATTERN CORRECTLY DETECTED AT THE END OF $INPUT_FUN\n" echo ${FILENAME%${PATTERN_FUN}} + file_exten_verif_name_recov_fun_RETURN=0 return 0 fi } \ No newline at end of file