bash read - ghdrako/doc_snipets GitHub Wiki
-
-a array
: stores the results of the word split operation in an array rather than separate variables -
–e
: use the Bash built-in Readline library to read the input line -
-s
: does not echo the input line to the standard output stream -
–p prompt
: print the prompt text before requesting the input from the standard input stream without a character -
–i text
: print the text as default input on the standard output stream (can only be used in combination with-e
) -
-d delim
: specify the delimiter of the input line rather than using the character -
–u fd
: read the input line from a given file descriptor -
–r
: treat the character as it is (cannot be used for escaping special characters) -
-t timeout
: attempt to read the input for a given period of seconds -
–N
: read exactly N characters from the input unless a timeout occurs or EOF is reached
read -p "What is your name? " name
echo “Your name is” $name
or
echo “Please type your name”
read name
echo “Your name is” $name
-
-s
flag can be used to hide the input of the user
read –s –p “Please type your password” $password
-
-n
flag can be used to add a constraint to the number of characters that the user may input. User can write n or less number of characters
read –n 8 –p “Please type your username not exceeding 8 characters” username
echo “Your username is” $username
-
-N
limits the user’s response to exaxtly number of character -
-a
flag - user input can also be taken in an array
echo “Please type your name, age, and email”
read –a array name age email
echo “Your name, age, and email address are: ${array[@]} name age email”
echo “Your name and age are: ${array[@]:0:1} name age”
echo “Your email address is: ${array[2]} email”
-
${array[@]}
will loop through all variables. - To iterate through the indexes 0 to 1, use
${array[@]:0:1}
with the variable names. - To obtain the value of a particular variable at a specific index, use
${array[2]}
with the variable name.
-
-t
flag timeout can be added as a condition of reading the code which makes the user enter information for a specific time. Otherwise, the program will move to the next line of code.
echo “What is the capital of Japan? Answer in 5 seconds”
read –t 5 answer
if [ “$answer” = “tokyo” ] || [ “$answer” = “Tokyo” ];
then
echo “Your answer is Correct!”
else
echo “Your answer is Wrong!”
fi
$cat file.csv
car,car model,car year,car vin
Mercury,Grand Marquis,2000,2G61S5S33F9986032
Mitsubishi,Truck,1995,SCFFDABE1CG137362
Now let’s consider extracting specific fields from this CSV file and printing specific fields out:
$ {
exec {file_descriptor}<"./file.csv"
declare -a input_array
while IFS="," read -a input_array -u $file_descriptor
do
echo "${input_array[0]},${input_array[2]}"
done
exec {file_descriptor}>&-
}
- call the exec built-in bash command to open a file descriptor on our input
- read command in a while loop which allows us to read the file line by line.
- close the file descriptor by calling again the exec command
- we defined the IFS as part of the while loop. This ensures that further word split operations outside the loop will use the default IFS
$ {
ls -ll / | { declare -a input
read
while read -a input;
do
echo "${input[0]} ${input[8]}"
done }
}
redirect the output of the ls command and extract the file names and their access rights from the ⁄ (root) folder
## Pierwotny sposób wyciągania deploymentów i ich wersji.
#kubectl --insecure-skip-tls-verify get deployments -n acp -o json | jq '.items[].spec.template.spec.containers[].image[30:100]' >> app_list_from_gke.txt
## Pobieram do zmiennych listę apek żeby poobcinać ilość znaków w wersji. SCDF pokazuje za dużo. W przypadku batch filtruje po defaultVersion == true.
batch_apps=$(curl -s -X GET https://$k8s_batch_mgmt/batch/apps/batch -H "$K8S_TOKEN" | jq '.[] | select(.defaultVersion == true) | (.name)+ ":" +(.version)' | tr -d '"' | sed 's/-RELEASE//')
online_apps=$(curl -s -X GET https://$k8s_batch_mgmt/batch/apps/deployments -H "$K8S_TOKEN" | jq '.[] | (.name)+ ":" +(.version)' | tr -d '"' | sed 's/-RELEASE//')
## BATCH LOOP
while IFS= read -r line; do
app_version=${line#*:}
app_name=${line%:*}
app_version=$(echo ${line#*:} | cut -d . -f -3 | sed 's/\./-/g')
echo "$app_name:$app_version" | tee -a ./$environment/app_list_from_gke.txt
done <<< "$batch_apps"
## DEPLOYEMNTS/ONLINE LOOP
while IFS= read -r line; do
app_version=${line#*:}
app_name=${line%:*}
app_version=$(echo ${line#*:} | cut -d . -f -3 | sed 's/\./-/g')
echo "$app_name:$app_version" | tee -a ./$environment/app_list_from_gke.txt
done <<< "$online_apps"