Snippets
Created by
Víctor Goñi Sanz
last modified
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | #!/bin/bash
# Check spell http://www.shellcheck.net/# online, or download, to test!
############################################################ Function declaration
### OPTIONS
f_template()
{
echo -e "\n--------------------------------------------------"
echo -e "--------------------------------------------------\n"
}
# This function, display usage info
f_display_usage()
{
echo -e "\n--------------------------------------------------"
echo "| This function teach you how this script works"
echo -e "| Usage:\n| $0" # -e option allow use \n
echo "| ${optionHelp} = Print usage"
echo "| ${optionTest} = Print a test with strings before and after"
echo "| ${optionRegExp} = Print regular expression"
echo "| ${optionSetInput} = Set input path, put before processing or default path is used"
echo "| ${optionSetOutput} = See output path, put before processing or default path is used"
echo "| ${optionGenerateInput} = Generate files in input path, if you want them"
echo "| ${optionPSP} = Process all files from input, remove PSP and dashes"
echo "| ${optionAllBrackets} = Process all files from input, remove parenthesis and brackets"
echo -e "--------------------------------------------------\n"
}
# Remove "PSP -" for all files given
f_remove_psp_at_start()
{
echo -e "\n--------------------------------------------------"
f_create_output
echo " Removing PSP - at beggining in files at path: ${inputpath}"
counter=0
for file in "${inputpath}"/* # * = All items, in output path
do
outfile=$(f_remove_characters_only "${file##*/}" "PSP")
outfile=$(f_remove_dash_only "${outfile}")
echo " Filename ${counter}: ${file##*/} --> ${outfile}"
cp "${file}" "${outputpath}/${outfile}"
let "counter += 1" # Forward
done
echo " ${counter} files processed! All files copied in output"
echo -e "--------------------------------------------------\n"
}
f_remove_parenthesis_and_brackets()
{
echo -e "\n--------------------------------------------------"
f_create_output
echo " Removing brackets in files at path: ${inputpath}"
counter=0
for file in "${inputpath}"/* # * = All items, in output path
do
filewithoutpath="${file##*/}" # Remove full path
filenameraw="${filewithoutpath%.*}" # Extract string before . from variable filename
extension="${filewithoutpath##*.}" # Extract string after . from variable filename
outfile=$(f_remove_spaces_at_end "${filenameraw}")
outfile=$(f_remove_parenthesis "${outfile}")
outfile=$(f_remove_square_brackets "${outfile}")
outfile=$(f_remove_curly_brackets "${outfile}")
outfile=$(f_remove_morethan2spaces "${outfile}")
echo " Filename ${counter}: ${file##*/} --> ${outfile}.${extension}"
cp "${file}" "${outputpath}/${outfile}.${extension}"
let "counter += 1" # Forward
done
echo " ${counter} files processed! All files copied in output"
echo -e "--------------------------------------------------\n"
}
# Sed info: http://www.grymoire.com/Unix/Sed.html#uh-4
f_test_sed()
{
inputvar="PSP - [square] The Answer (was) is 42 (or 43) this answer is {Correct} or {or not} [works] "
temp=$(f_remove_spaces_at_end "${inputvar}")
echo -e " - f_remove_spaces_at_end\n · ${inputvar}\n * ${temp}"
temp=$(f_remove_parenthesis "${inputvar}")
echo -e " - f_remove_parenthesis\n · ${inputvar}\n * ${temp}"
temp=$(f_remove_curly_brackets "${inputvar}")
echo -e " - f_remove_curly_brackets\n · ${inputvar}\n * ${temp}"
temp=$( f_remove_square_brackets "${inputvar}")
echo -e " - f_remove_square_brackets\n · ${inputvar}\n * ${temp}"
temp=$( f_remove_morethan2spaces "${inputvar}")
echo -e " - f_remove_morethan2spaces\n · ${inputvar}\n * ${temp}"
temp=$(f_remove_parenthesis_only "${inputvar}")
echo -e " - f_remove_parenthesis_only\n · ${inputvar}\n * ${temp}"
temp=$(f_remove_characters_only "${inputvar}" "PSP")
echo -e " - f_remove_characters_only\n · ${inputvar}\n * ${temp}"
temp=$(f_remove_dash_only "${inputvar}" )
echo -e " - f_remove_dash_only\n · ${inputvar}\n * ${temp}"
temp=$(f_remove_square_brackets_only "${inputvar}")
echo -e " - f_remove_square_brackets_only\n · ${inputvar}\n * ${temp}"
echo -e " - Concatenate functions"
temp=$(f_remove_parenthesis "${inputvar}")
temp=$(f_remove_square_brackets "${temp}")
temp=$(f_remove_curly_brackets "${temp}")
temp=$(f_remove_characters_only "${temp}" "PSP")
temp=$(f_remove_dash_only "${temp}")
echo -e " - Result: ${temp}"
#
echo -e "--------------------------------------------------\n"
}
f_test_regularexpresion()
{
echo -e "\n--------------------------------------------------"
echo " Printing different regular expression..."
inputvar=$(echo {A..z})
echo " · {A..z} = ${inputvar} "
inputvar=$(echo {A..Z})
echo " · {A..Z} = ${inputvar} "
inputvar=$(echo {a..z})
echo " · {A..z} = ${inputvar} "
inputvar=$(echo {0..9})
echo " · {0..9} = ${inputvar} "
inputvar=$(echo {0..9} & echo {A..z})
echo " · {0..9} & {A..z} = ${inputvar} "
#
echo -e "--------------------------------------------------\n"
}
f_generate_files()
{
echo -e "\n--------------------------------------------------"
f_create_input
touch ${inputpath}/"file1.dat"
touch ${inputpath}/"file1(1).dat"
touch ${inputpath}/"file12(2).dat"
touch ${inputpath}/"file12(2)(3).dat"
touch ${inputpath}/"PSP - file7.dat"
touch ${inputpath}/"PSP - file2 [as].dat"
touch ${inputpath}/"PSP - file3 {asd} .dat"
touch ${inputpath}/"file4[!] [ao]{32}.dat"
touch ${inputpath}/"file5[!] [ao](32).dat"
touch ${inputpath}/"file6[ao]{32}(ds12).dat"
echo -e "--------------------------------------------------\n"
}
### Other functions
# Check if exist input directory and create if not
f_create_input()
{
if [ ! -d "${inputpath}" ] # d for directory
then
echo " Creating input folder..."
mkdir ${inputpath}
fi
}
# Check if exist output directory and create if not
f_create_output()
{
if [ ! -d "${outputpath}" ] # d for directory
then
echo " Creating output folder..."
mkdir ${outputpath}
fi
}
# Check if are provided correct arguments
f_evaluate_options()
{
echo " Evaluation options, number of arguments: ${Nargs}"
eval set -- ${options}
echo " Options: ${options}"
}
# Remove parenthesis (*) and content inside!
f_remove_parenthesis()
{
temp=$(echo "${1}" | sed s/'([^)]*)'/''/g)
echo ${temp}
}
# Remove braces or curly brackets {*} and content inside!
f_remove_curly_brackets()
{
temp=$(echo "${1}" | sed ':again;$!N;$!b again; s/{[^}]*}//g')
echo ${temp}
}
# Remove square brackets [*] and content inside!
f_remove_square_brackets()
{
temp=$(echo "${1}" | sed 's/\[[^]]*\]//g' )
echo ${temp}
}
f_remove_morethan2spaces()
{
temp=$(echo "${1}" | sed -r s/'\s{2,}'/' '/g )
echo ${temp}
}
f_remove_spaces_at_end()
{
temp=$(echo "${1}" | sed 's/[[:blank:]]*$//' )
echo ${temp}
}
f_remove_dash_only()
{
temp=$(echo "${1}" | sed 's/\-/ /' )
echo ${temp}
}
f_remove_parenthesis_only()
{
temp=$(echo "${1}" | sed 's/[()]//g' )
echo ${temp}
}
f_remove_characters_only()
{
temp=$(echo "${1}" | sed -r 's/\<'${2}'\>//g' )
echo ${temp}
}
f_remove_square_brackets_only()
{
temp=$(echo "${1}" | sed 's/\[//g;s/\]//g' )
echo ${temp}
}
############################################################ End Function declaration
# Main program
clear # clear terminal
# First of all, store arguments in a variable.
# Bash have variable when call an executable, $0 = CommandName, $1..$N = Arguments
# We store arguments in an array. If a function is called, you cannot access to them if not are global variables
# because they are overwrited by function arguments.
args=(${@}) # Arguments
Nargs=(${#}) # Number of arguments
# We define options, using getopt
# -o Each single character stands for an option
# -l Long name for each option
# : after an option (short or long) tells that option is required argument
# :: tells is a optional argument
# For extension take a look here: http://www.bahmanm.com/blogs/command-line-options-how-to-parse-in-bash-using-getopt
options=$(getopt -o hti:o:grpa -l help,test,input:,output:,generate,regexp,psp,all -- "${@}")
if [[ $? -ne 0 ]]
then
echo " Error: Detected invalid option"
fi
# -o hs:ao = h & a option, no arguments; s option with a required option;
# -l help,say:,author = same.
# Declarate variables with options to clarify
optionHelp="-h|--help"
optionSetInput="-i|--input \"path\""
optionSetOutput="-o|--output \"path\""
optionGenerateInput="-g|--generate"
optionTest="-t|--test"
optionRegExp="-r|--regexp"
optionPSP="-p|--psp"
optionAllBrackets="-a|--all"
# We define some global variables
commandName=$(basename "$0") # Extract name from first variable
commandPath=$(pwd) # Get the path with command pwd
outputpath="./output" # Output folder by default
inputpath="./input" # input folder by default
temp="" # Temporal string to process names
echo "------- My third script begins execution..."
echo "--------------------------------------------"
echo " Executing command: ${commandName}"
echo " At path: ${commandPath}"
# Call a function to evaluate arguments as options
f_evaluate_options
# Check all arguments
# We are going to check the all the options shifting them.
# Using shift, the positional parameters are shifted to the left by this number, N.
# Using case syntaxis, more info: http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_03.html
while [ "$1" != "" ]
do
echo "Processing option: ${1}"
case "$1" in
-h|--help) f_display_usage ;; # Call function
-t|--test) f_test_sed ;; # Call function
-r|--regexp) f_test_regularexpresion ;; # Call function
-i|--input) inputpath=${2} ; echo " Setting Input path: ${2}" ; shift ;; # Call function, reading next variable and shift
-o|--output) outputpath=${2} ; echo " Setting Output path: ${2}" ; shift ;; # Call function, reading next variable and shift
-g|--generate) f_generate_files ;; # Call function
-p|--psp) f_remove_psp_at_start ;; # Call function
-a|--all) f_remove_parenthesis_and_brackets ;; # Call function
*) echo "Unknown option: $1" ; f_display_usage ; break ;;
esac
shift; # Advance
done;
echo "------- My third script end now!"
echo "---------------------------------"
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.