From 57934f602c4610a2dcf1ba04c83fca2bb49b77f5 Mon Sep 17 00:00:00 2001 From: Sorin Zamfir Date: Sun, 1 Dec 2019 23:00:04 +0200 Subject: [PATCH 1/8] BAEL-3370: First examples --- linux-bash/read/src/main/dummy_file | 7 +++ linux-bash/read/src/main/read_inputs.sh | 83 +++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 linux-bash/read/src/main/dummy_file create mode 100755 linux-bash/read/src/main/read_inputs.sh diff --git a/linux-bash/read/src/main/dummy_file b/linux-bash/read/src/main/dummy_file new file mode 100644 index 0000000000..4c581fc816 --- /dev/null +++ b/linux-bash/read/src/main/dummy_file @@ -0,0 +1,7 @@ +Lorem Ipsum is simply dummy text of the printing and typesetting industry. +Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, +when an unknown printer took a galley of type and scrambled it to make a type specimen book. +It has survived not only five centuries, but also the leap into electronic typesetting, +remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset +sheets containing Lorem Ipsum passages, and more recently with desktop publishing software +like Aldus PageMaker including versions of Lorem Ipsum. \ No newline at end of file diff --git a/linux-bash/read/src/main/read_inputs.sh b/linux-bash/read/src/main/read_inputs.sh new file mode 100755 index 0000000000..4351a27e60 --- /dev/null +++ b/linux-bash/read/src/main/read_inputs.sh @@ -0,0 +1,83 @@ +#!/bin/bash + + +default_read() { + echo "Please enter something:" + read first second third + echo "first word [$first]" + echo "second word [$second]" + echo "third word [$third]" +} + +array_read() { + declare -a input_array + echo "Please enter something:" + read -a input_array + for input in ${input_array[@]} + do + echo " Word [$input]" + done +} + +special_delim() { + echo "Please enter something:" + read -d ";" first second third + echo "first word [$first]" +} + +file_read(){ + # open file descriptor for reading + exec {file_descriptor}<"./dummy_file" + declare -a input_array + echo "Reading first line from file" + read -a input_array -u $file_descriptor + for input in ${input_array[@]} + do + echo " Word [$input]" + done + exec {file_descriptor}>&- +} + +prompt_read(){ + echo "With prompt :" + prompt="You shall not pass:" + read -p "$prompt" input + echo "word -> [$input]" +} + +default_input_read(){ + echo "Default input read:" + prompt=$'What\'s up doc: \n' + default_input="Nothing much just killing time" + read -e -p "$prompt" -i "$default_input" actual_input + echo "word -> [$actual_input]" +} + +advanced_pipeing(){ + ls | (read -p "Input from ls" input; echo "Single read -> [$input]") + ls | (while IFS= read input; + do + echo "$input" + done ) + # process substitution + while read input + do + echo "$input" + done < <(ls) + +} + +#default_read +#array_read +#special_delim +#file_read +#prompt_read +#default_input_read +advanced_pipeing + + + + + + + From d64771257a20b589abdac875a5df88cbd185c4fb Mon Sep 17 00:00:00 2001 From: Sorin Zamfir Date: Tue, 3 Dec 2019 20:56:35 +0200 Subject: [PATCH 2/8] BAEL-3370: Some more examples --- linux-bash/read/src/main/file.csv | 2 ++ linux-bash/read/src/main/read_inputs.sh | 36 ++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 linux-bash/read/src/main/file.csv diff --git a/linux-bash/read/src/main/file.csv b/linux-bash/read/src/main/file.csv new file mode 100644 index 0000000000..903384a5b6 --- /dev/null +++ b/linux-bash/read/src/main/file.csv @@ -0,0 +1,2 @@ +lorem;ipsum;is\;simply\;du\mmy;text +of;the;printing;and;typesetting;industry. diff --git a/linux-bash/read/src/main/read_inputs.sh b/linux-bash/read/src/main/read_inputs.sh index 4351a27e60..d0dd2c3ea9 100755 --- a/linux-bash/read/src/main/read_inputs.sh +++ b/linux-bash/read/src/main/read_inputs.sh @@ -35,6 +35,7 @@ file_read(){ do echo " Word [$input]" done + # close file descriptor after reading exec {file_descriptor}>&- } @@ -67,13 +68,46 @@ advanced_pipeing(){ } +custom_ifs_read(){ + declare -a input_array + while IFS=";" read -a input_array -r + do + for element in ${input_array[@]} + do + echo -n "$element.backup " + done + echo "" + done < "file.csv" +} + +infinite_read(){ + while read -d "?" -ep "Input:" input + do + echo "$input" + done +} + +custom_ifs(){ + declare -a input_array + IFS=$1 # whitespace + read -a input_array -p "Enter something with delimiter <$1>:" + for input in ${input_array[@]} + do + echo "[$input]" + done +} + #default_read #array_read #special_delim #file_read #prompt_read #default_input_read -advanced_pipeing +#advanced_pipeing +#custom_ifs_read +#infinite_read +custom_ifs " " +custom_ifs ";" From 5614955b1395e76d688aa22ee767f17e08cf78c3 Mon Sep 17 00:00:00 2001 From: Sorin Zamfir Date: Fri, 6 Dec 2019 00:26:42 +0200 Subject: [PATCH 3/8] BAEL-3370: Organized the sample scripts --- linux-bash/read/src/main/file.csv | 3 +- linux-bash/read/src/main/read_inputs.sh | 84 +++++++++++++------------ 2 files changed, 45 insertions(+), 42 deletions(-) diff --git a/linux-bash/read/src/main/file.csv b/linux-bash/read/src/main/file.csv index 903384a5b6..69a50772ab 100644 --- a/linux-bash/read/src/main/file.csv +++ b/linux-bash/read/src/main/file.csv @@ -1,2 +1 @@ -lorem;ipsum;is\;simply\;du\mmy;text -of;the;printing;and;typesetting;industry. +car,car model,car year,car vin;Mercury,Grand Marquis,2000,2G61S5S33F9986032;Mitsubishi,Truck,1995,SCFFDABE1CG137362;Ford,Mustang,1968,2G4WS55J351278031;Ford,Crown Victoria,1996,4T1BK1EB8EU586249;GMC,Envoy,2004,WVGEF9BP3FD720618; diff --git a/linux-bash/read/src/main/read_inputs.sh b/linux-bash/read/src/main/read_inputs.sh index d0dd2c3ea9..90d08b4199 100755 --- a/linux-bash/read/src/main/read_inputs.sh +++ b/linux-bash/read/src/main/read_inputs.sh @@ -1,50 +1,48 @@ #!/bin/bash - -default_read() { - echo "Please enter something:" - read first second third - echo "first word [$first]" - echo "second word [$second]" - echo "third word [$third]" +# Section 2.3 +prompt_read_password(){ + prompt="You shall not pass:" + read -p "$prompt" -s input + echo -e "\ninput password [$input]" } array_read() { declare -a input_array - echo "Please enter something:" - read -a input_array + text="baeldung is a cool tech site" + read -e -i "$text" -a input_array for input in ${input_array[@]} do - echo " Word [$input]" + echo " word [$input]" done } -special_delim() { - echo "Please enter something:" - read -d ";" first second third - echo "first word [$first]" +# section 2.2 +custom_ifs_no_array(){ + IFS=";" + read input1 input2 input3 + echo "[$input1] [$input2] [$input3]" } +# section 2.1 +default_read() { + read input1 input2 input3 + echo "[$input1] [$input2] [$input3]" +} + +# section 3.1 file_read(){ - # open file descriptor for reading - exec {file_descriptor}<"./dummy_file" + exec {file_descriptor}<"./file.csv" declare -a input_array - echo "Reading first line from file" - read -a input_array -u $file_descriptor - for input in ${input_array[@]} + delimiter=";" + while IFS="," read -a input_array -d $delimiter -u $file_descriptor do - echo " Word [$input]" + echo "${input_array[0]},${input_array[2]}" done - # close file descriptor after reading exec {file_descriptor}>&- } -prompt_read(){ - echo "With prompt :" - prompt="You shall not pass:" - read -p "$prompt" input - echo "word -> [$input]" -} + default_input_read(){ echo "Default input read:" @@ -55,17 +53,20 @@ default_input_read(){ } advanced_pipeing(){ - ls | (read -p "Input from ls" input; echo "Single read -> [$input]") - ls | (while IFS= read input; - do - echo "$input" - done ) - # process substitution - while read input - do - echo "$input" - done < <(ls) + ls -ll | ( declare -a input + while read -a input; + do + echo "${input[0]} ${input[8]}" + done ) +} +advanced_pipeing_ps(){ +# process substitution + declare -a input + while read -a input + do + echo "${input[0]} ${input[8]}" + done < <(ls -ll) } custom_ifs_read(){ @@ -97,17 +98,20 @@ custom_ifs(){ done } + + #default_read #array_read #special_delim -#file_read +file_read #prompt_read #default_input_read #advanced_pipeing #custom_ifs_read #infinite_read -custom_ifs " " -custom_ifs ";" +# custom_ifs " " +# custom_ifs ";" +#custom_ifs_no_array From 7da51d09afb37afe7782c9b9bfbff63d33743bcf Mon Sep 17 00:00:00 2001 From: Sorin Zamfir Date: Sun, 8 Dec 2019 23:47:06 +0200 Subject: [PATCH 4/8] BAEL-3370: Updated with some more samples --- linux-bash/read/src/main/read_inputs.sh | 35 ++++++++++++++++--------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/linux-bash/read/src/main/read_inputs.sh b/linux-bash/read/src/main/read_inputs.sh index 90d08b4199..3e071133aa 100755 --- a/linux-bash/read/src/main/read_inputs.sh +++ b/linux-bash/read/src/main/read_inputs.sh @@ -42,24 +42,35 @@ file_read(){ exec {file_descriptor}>&- } - - -default_input_read(){ - echo "Default input read:" - prompt=$'What\'s up doc: \n' - default_input="Nothing much just killing time" - read -e -p "$prompt" -i "$default_input" actual_input - echo "word -> [$actual_input]" -} - +# section 3.2 advanced_pipeing(){ - ls -ll | ( declare -a input + ls -ll | { declare -a input + read while read -a input; do echo "${input[0]} ${input[8]}" - done ) + done } } +# section 3.3 +timeout_input_read(){ + prompt="You shall not pass:" + read -p "$prompt" -s -r -t 5 input + if [ -z "$input" ]; then + echo -e "\ntimeout occured!" + else + echo -e "\ninput word [$input]" + fi +} + +exactly_n_read(){ + prompt="Reading exactly 11 chars:" + read -p "$prompt" -N 11 -t 5 input1 input2 + echo -e "\ninput word1 [$input1]" + echo "input word2 [$input2]" +} + + advanced_pipeing_ps(){ # process substitution declare -a input From a2b9b697cbbd133a3cb8a769f261d628d7b27988 Mon Sep 17 00:00:00 2001 From: Sorin Zamfir Date: Mon, 9 Dec 2019 22:30:42 +0200 Subject: [PATCH 5/8] BAEL-3370: Cleaned up examples --- linux-bash/read/src/main/{ => bash}/file.csv | 0 .../read/src/main/{ => bash}/read_inputs.sh | 91 +++---------------- linux-bash/read/src/main/dummy_file | 7 -- 3 files changed, 15 insertions(+), 83 deletions(-) rename linux-bash/read/src/main/{ => bash}/file.csv (100%) rename linux-bash/read/src/main/{ => bash}/read_inputs.sh (60%) delete mode 100644 linux-bash/read/src/main/dummy_file diff --git a/linux-bash/read/src/main/file.csv b/linux-bash/read/src/main/bash/file.csv similarity index 100% rename from linux-bash/read/src/main/file.csv rename to linux-bash/read/src/main/bash/file.csv diff --git a/linux-bash/read/src/main/read_inputs.sh b/linux-bash/read/src/main/bash/read_inputs.sh similarity index 60% rename from linux-bash/read/src/main/read_inputs.sh rename to linux-bash/read/src/main/bash/read_inputs.sh index 3e071133aa..c39f77f217 100755 --- a/linux-bash/read/src/main/read_inputs.sh +++ b/linux-bash/read/src/main/bash/read_inputs.sh @@ -1,5 +1,18 @@ #!/bin/bash +# section 2.1 +default_read() { + read input1 input2 input3 + echo "[$input1] [$input2] [$input3]" +} + +# section 2.2 +custom_ifs_no_array(){ + IFS=";" + read input1 input2 input3 + echo "[$input1] [$input2] [$input3]" +} + # Section 2.3 prompt_read_password(){ prompt="You shall not pass:" @@ -17,19 +30,6 @@ array_read() { done } -# section 2.2 -custom_ifs_no_array(){ - IFS=";" - read input1 input2 input3 - echo "[$input1] [$input2] [$input3]" -} - -# section 2.1 -default_read() { - read input1 input2 input3 - echo "[$input1] [$input2] [$input3]" -} - # section 3.1 file_read(){ exec {file_descriptor}<"./file.csv" @@ -43,7 +43,7 @@ file_read(){ } # section 3.2 -advanced_pipeing(){ +command_pipe(){ ls -ll | { declare -a input read while read -a input; @@ -68,65 +68,4 @@ exactly_n_read(){ read -p "$prompt" -N 11 -t 5 input1 input2 echo -e "\ninput word1 [$input1]" echo "input word2 [$input2]" -} - - -advanced_pipeing_ps(){ -# process substitution - declare -a input - while read -a input - do - echo "${input[0]} ${input[8]}" - done < <(ls -ll) -} - -custom_ifs_read(){ - declare -a input_array - while IFS=";" read -a input_array -r - do - for element in ${input_array[@]} - do - echo -n "$element.backup " - done - echo "" - done < "file.csv" -} - -infinite_read(){ - while read -d "?" -ep "Input:" input - do - echo "$input" - done -} - -custom_ifs(){ - declare -a input_array - IFS=$1 # whitespace - read -a input_array -p "Enter something with delimiter <$1>:" - for input in ${input_array[@]} - do - echo "[$input]" - done -} - - - -#default_read -#array_read -#special_delim -file_read -#prompt_read -#default_input_read -#advanced_pipeing -#custom_ifs_read -#infinite_read -# custom_ifs " " -# custom_ifs ";" -#custom_ifs_no_array - - - - - - - +} \ No newline at end of file diff --git a/linux-bash/read/src/main/dummy_file b/linux-bash/read/src/main/dummy_file deleted file mode 100644 index 4c581fc816..0000000000 --- a/linux-bash/read/src/main/dummy_file +++ /dev/null @@ -1,7 +0,0 @@ -Lorem Ipsum is simply dummy text of the printing and typesetting industry. -Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, -when an unknown printer took a galley of type and scrambled it to make a type specimen book. -It has survived not only five centuries, but also the leap into electronic typesetting, -remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset -sheets containing Lorem Ipsum passages, and more recently with desktop publishing software -like Aldus PageMaker including versions of Lorem Ipsum. \ No newline at end of file From 9818939e402d9a2c97befa1a1deb32f1b94cbba1 Mon Sep 17 00:00:00 2001 From: Sorin Zamfir Date: Mon, 16 Dec 2019 21:23:30 +0200 Subject: [PATCH 6/8] BAEL-3370: Fixed formatting --- linux-bash/read/src/main/bash/read_inputs.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/linux-bash/read/src/main/bash/read_inputs.sh b/linux-bash/read/src/main/bash/read_inputs.sh index c39f77f217..6e35e5de96 100755 --- a/linux-bash/read/src/main/bash/read_inputs.sh +++ b/linux-bash/read/src/main/bash/read_inputs.sh @@ -1,7 +1,7 @@ #!/bin/bash # section 2.1 -default_read() { +default_read(){ read input1 input2 input3 echo "[$input1] [$input2] [$input3]" } @@ -20,7 +20,7 @@ prompt_read_password(){ echo -e "\ninput password [$input]" } -array_read() { +array_read(){ declare -a input_array text="baeldung is a cool tech site" read -e -i "$text" -a input_array From 18b9e796efde0009a917f4805787d20dee852689 Mon Sep 17 00:00:00 2001 From: Sorin Zamfir Date: Tue, 17 Dec 2019 23:30:12 +0200 Subject: [PATCH 7/8] BAEL-3370: First draft of menu --- linux-bash/read/src/main/bash/read_inputs.sh | 24 +++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/linux-bash/read/src/main/bash/read_inputs.sh b/linux-bash/read/src/main/bash/read_inputs.sh index 6e35e5de96..199b9b6a4f 100755 --- a/linux-bash/read/src/main/bash/read_inputs.sh +++ b/linux-bash/read/src/main/bash/read_inputs.sh @@ -58,6 +58,7 @@ timeout_input_read(){ read -p "$prompt" -s -r -t 5 input if [ -z "$input" ]; then echo -e "\ntimeout occured!" + echo "empty -?> [$input]" else echo -e "\ninput word [$input]" fi @@ -68,4 +69,25 @@ exactly_n_read(){ read -p "$prompt" -N 11 -t 5 input1 input2 echo -e "\ninput word1 [$input1]" echo "input word2 [$input2]" -} \ No newline at end of file +} + + +echo "Read command samples" +options=("default_read" "custom_ifs_no_array" "prompt_read_password" "quit") +select option in "${options[@]}" +do + case $option in + "default_read") + echo "Default read" + default_read + ;; + "custom_ifs_no_array") + echo "Custom IFS no array" + custom_ifs_no_array + ;; + "quit") + break + ;; + *) echo "Invalid option";; + esac +done \ No newline at end of file From f60b9019b8c1ab6cc887b6ed6699280a8a3f13dd Mon Sep 17 00:00:00 2001 From: Sorin Zamfir Date: Wed, 18 Dec 2019 20:50:17 +0200 Subject: [PATCH 8/8] BAEL-3370: Included easy menu for samples --- linux-bash/read/src/main/bash/read_inputs.sh | 43 ++++++++++++++++---- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/linux-bash/read/src/main/bash/read_inputs.sh b/linux-bash/read/src/main/bash/read_inputs.sh index 199b9b6a4f..30eb598ac3 100755 --- a/linux-bash/read/src/main/bash/read_inputs.sh +++ b/linux-bash/read/src/main/bash/read_inputs.sh @@ -8,9 +8,12 @@ default_read(){ # section 2.2 custom_ifs_no_array(){ + OLDIFS=$IFS IFS=";" read input1 input2 input3 echo "[$input1] [$input2] [$input3]" + # restore default IFS after we're finished so current shell behaves like before + IFS=$OLDIFS } # Section 2.3 @@ -44,7 +47,7 @@ file_read(){ # section 3.2 command_pipe(){ - ls -ll | { declare -a input + ls -ll / | { declare -a input read while read -a input; do @@ -58,7 +61,6 @@ timeout_input_read(){ read -p "$prompt" -s -r -t 5 input if [ -z "$input" ]; then echo -e "\ntimeout occured!" - echo "empty -?> [$input]" else echo -e "\ninput word [$input]" fi @@ -71,20 +73,47 @@ exactly_n_read(){ echo "input word2 [$input2]" } - -echo "Read command samples" -options=("default_read" "custom_ifs_no_array" "prompt_read_password" "quit") +# main menu entry point +echo "****Read command samples menu*****" +PS3="Your choice (1,2,3 etc.):" +options=("default_read" "custom_ifs_no_array" "prompt_read_password" \ + "array_read" "file_read" "command_pipe" "timeout_input_read" \ + "exactly_n_read" "quit") select option in "${options[@]}" do case $option in "default_read") - echo "Default read" + echo "Enter something separated by spaces" default_read ;; "custom_ifs_no_array") - echo "Custom IFS no array" + echo "Enter something separated by ;" custom_ifs_no_array ;; + "prompt_read_password") + echo "Enter an invisible password after the prompt" + prompt_read_password + ;; + "array_read") + echo "Enter something else or just return" + array_read + ;; + "file_read") + echo "Reading from one liner csv file" + file_read + ;; + "command_pipe") + echo "Listing files and access rights from /" + command_pipe + ;; + "timeout_input_read") + echo "Enter something in 5 seconds or less" + timeout_input_read + ;; + "exactly_n_read") + echo "Enter at least 11 characters or wait 5 seconds" + exactly_n_read + ;; "quit") break ;;