2019-12-01 16:00:04 -05:00
|
|
|
#!/bin/bash
|
|
|
|
|
2019-12-09 15:30:42 -05:00
|
|
|
# section 2.1
|
2019-12-16 14:23:30 -05:00
|
|
|
default_read(){
|
2019-12-09 15:30:42 -05:00
|
|
|
read input1 input2 input3
|
|
|
|
echo "[$input1] [$input2] [$input3]"
|
|
|
|
}
|
|
|
|
|
|
|
|
# section 2.2
|
|
|
|
custom_ifs_no_array(){
|
2019-12-18 13:50:17 -05:00
|
|
|
OLDIFS=$IFS
|
2019-12-09 15:30:42 -05:00
|
|
|
IFS=";"
|
|
|
|
read input1 input2 input3
|
|
|
|
echo "[$input1] [$input2] [$input3]"
|
2019-12-18 13:50:17 -05:00
|
|
|
# restore default IFS after we're finished so current shell behaves like before
|
|
|
|
IFS=$OLDIFS
|
2019-12-09 15:30:42 -05:00
|
|
|
}
|
|
|
|
|
2019-12-05 17:26:42 -05:00
|
|
|
# Section 2.3
|
|
|
|
prompt_read_password(){
|
|
|
|
prompt="You shall not pass:"
|
|
|
|
read -p "$prompt" -s input
|
|
|
|
echo -e "\ninput password [$input]"
|
2019-12-01 16:00:04 -05:00
|
|
|
}
|
|
|
|
|
2019-12-16 14:23:30 -05:00
|
|
|
array_read(){
|
2019-12-01 16:00:04 -05:00
|
|
|
declare -a input_array
|
2019-12-05 17:26:42 -05:00
|
|
|
text="baeldung is a cool tech site"
|
|
|
|
read -e -i "$text" -a input_array
|
2019-12-01 16:00:04 -05:00
|
|
|
for input in ${input_array[@]}
|
|
|
|
do
|
2019-12-05 17:26:42 -05:00
|
|
|
echo " word [$input]"
|
2019-12-01 16:00:04 -05:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2019-12-05 17:26:42 -05:00
|
|
|
# section 3.1
|
2019-12-01 16:00:04 -05:00
|
|
|
file_read(){
|
2019-12-05 17:26:42 -05:00
|
|
|
exec {file_descriptor}<"./file.csv"
|
2019-12-01 16:00:04 -05:00
|
|
|
declare -a input_array
|
2019-12-05 17:26:42 -05:00
|
|
|
delimiter=";"
|
|
|
|
while IFS="," read -a input_array -d $delimiter -u $file_descriptor
|
2019-12-01 16:00:04 -05:00
|
|
|
do
|
2019-12-05 17:26:42 -05:00
|
|
|
echo "${input_array[0]},${input_array[2]}"
|
2019-12-01 16:00:04 -05:00
|
|
|
done
|
|
|
|
exec {file_descriptor}>&-
|
|
|
|
}
|
|
|
|
|
2019-12-08 16:47:06 -05:00
|
|
|
# section 3.2
|
2019-12-09 15:30:42 -05:00
|
|
|
command_pipe(){
|
2019-12-18 13:50:17 -05:00
|
|
|
ls -ll / | { declare -a input
|
2019-12-08 16:47:06 -05:00
|
|
|
read
|
2019-12-05 17:26:42 -05:00
|
|
|
while read -a input;
|
|
|
|
do
|
|
|
|
echo "${input[0]} ${input[8]}"
|
2019-12-08 16:47:06 -05:00
|
|
|
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
|
2019-12-05 17:26:42 -05:00
|
|
|
}
|
2019-12-01 16:00:04 -05:00
|
|
|
|
2019-12-08 16:47:06 -05:00
|
|
|
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]"
|
2019-12-17 16:30:12 -05:00
|
|
|
}
|
|
|
|
|
2019-12-18 13:50:17 -05:00
|
|
|
# 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")
|
2019-12-17 16:30:12 -05:00
|
|
|
select option in "${options[@]}"
|
|
|
|
do
|
|
|
|
case $option in
|
|
|
|
"default_read")
|
2019-12-18 13:50:17 -05:00
|
|
|
echo "Enter something separated by spaces"
|
2019-12-17 16:30:12 -05:00
|
|
|
default_read
|
|
|
|
;;
|
|
|
|
"custom_ifs_no_array")
|
2019-12-18 13:50:17 -05:00
|
|
|
echo "Enter something separated by ;"
|
2019-12-17 16:30:12 -05:00
|
|
|
custom_ifs_no_array
|
|
|
|
;;
|
2019-12-18 13:50:17 -05:00
|
|
|
"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
|
|
|
|
;;
|
2019-12-17 16:30:12 -05:00
|
|
|
"quit")
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
*) echo "Invalid option";;
|
|
|
|
esac
|
|
|
|
done
|