Combining loops and file redirection [closed] - bash

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to create a script that will prompt the user to enter a file name, request the user to enter four peoples names, then sort the names into the file specified.

You can use this code:
#!/bin/bash
read -p "Enter filename: " fn
declare -a arr
for ((i=1; i<=4; i++)); do
read -p "Enter name $i: " n
arr+=("$n")
done
sort <(printf "%s\n" ${arr[#]}) > "$fn"

Related

How to slow down the cat command without piping the output to the more command? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 1 year ago.
Improve this question
Bash version 4.4.0
Ubuntu 18.04.5 LTS
Is there a way to slow down the cat command to a crawl so I can visually see the output of a file without piping the output into the more command or another file?
A bash specific approach with command substitution.
while IFS= read -rn1 -u9; do printf '%s' "${REPLY:-$'\n'}"; sleep .05 ; done 9< <(help for)
If it is a file.
while IFS= read -rn1 -u9; do printf '%s' "${REPLY:-$'\n'}"; sleep .05 ; done 9< file.txt
It looks more like reading while someone is typing.
I doubt cat itself has any parameters that can slowdown display. Script like this probably can :
#!/bin/bash
while read -r i; do echo "$i"; sleep 0.3;done< my-file.txt

Ask a user for integers until they enter control+a. Then sum up the integers and display the total [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I've been working on some bash scripts and kind of hit a wall.
I need to ask a user to enter a bunch of different integers, and when they press 'control+a'
it'll sum up every integer they entered, like this:
10
10
2
"control+a"
22
I'm not sure where to even start on this.
I really appreciate the help, thank you.
If your terminal supports it:
#!/bin/bash
stty eof ^A
back2default(){ stty eof ^D; }
trap back2default EXIT
declare -i sum=0
while true; do
read -r foo
[[ -z $foo ]] && break
sum+=$foo
done
echo "sum: $sum"

Generating arrays from command response with Bash [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
In my shell, I am running the following command:
RESPONSE=($(chrome-cli list windows))
My response from this looks like this:
[32] Reading output of a command into an array in Bash - Stack Overflow
[52] Apple
What I am trying to do is create an array from the response like so: [32, 52] so I can iterate over them to run more commands.
I have tried a good few things but I am getting nowhere.
As I understand you want an array that contains windows' IDs.
ar=($(chrome-cli list windows | cut -d " " -f 1 | sed 's/.$//; s/^.//'))
echo ${ar[#]}
In your example output will be 32 52
That could be a somehow ugly solution:
echo "$a" #no chrome-cli in my system
[32] Reading output of a command into an array in Bash - Stack Overflow
[52] Apple
declare -a printf "arr=($(sed 's/] /]=\"/g; s/$/\"/g; s/$\n//g' <<<"$a"))"
echo "${arr[32]}"
Reading output of a command into an array in Bash - Stack Overflow
echo "${arr[52]}"
Apple
declare -p arr
#Result: declare -a arr=([32]="Reading output of a command into an array in Bash - Stack Overflow" [52]="Apple")

Can grep track patterns it finds? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a very long pattern file and medium-length text file. I simply want to know if the strings in the pattern file are present or not -- I don't care what line they're on. Is there a way to track which patterns are found and which not?
You can do something like this:
while read line; do
grep -q "$line" textFile
echo "${line}: $?"
done < patternFile
Loop over the patternFile and for every pattern invoke a grep -q on the textFile. grep -q will not produce any output, but it will set bash's exit status to 0 if the pattern was found and to 1 if it was not found.
As commented by that other guy, you can get a list with all matching patterns like this:
while read line; do
grep -q "$line" textFile && echo "$line"
done < patternFile

How to read multi-line input in a Bash script? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want store in a file and in a variable multiples lines from a "paste" via shell script. A simple read terminates after the first line.
How can I accomplish that?
Example:
echo "Paste the certificate key:"
1fv765J85HBPbPym059qseLx2n5MH4NPKFhgtuFCeU2ep6B19chh4RY7ZmpXIvXrS7348y0NdwiYT61
1RkW75vBjGiNZH4Y9HxXfQ2VShKS70znTLxlRPfn3I7zvNZW3m3zQ1NzG62Gj1xrdPD7M2rdE2AcOr3
Pud2ij43br4K3729gbG4n19Ygx5NGI0212eHN154RuC4MtS4qmRphb2O9FJgzK8IcFW0sTn71niwLyi
JOqBQmA5KtbjV34vp3lVBKCZp0PVJ4Zcy7fd5R1Fziseux4ncio32loIne1a7MPVqyIuJ8yv5IJ6s5P
485YQX0ll7hUgqepiz9ejIupjZb1003B7NboGJMga2Rllu19JC0pn4OmrnxfN025RMU6Qkv54v2fqfg
UmtbXV2mb4IuoBo113IgUg0bh8n2bhZ768Iiw2WMaemgGR6XcQWi0T6Fvg0MkiYELW2ia1oCO83sK06
2X05sU4Lv9XeV7BaOtC8Y5W7vgqxu69uwsFALripdZS7C8zX1WF6XvFGn4iFF1e5K560nooInX514jb
0SI6B1m771vqoDA73u1ZjbY7SsnS07eLxp96GrHDD7573lbJXJa4Uz3t0LW2dCWNy6H3YmojVXQVYA1
v3TPxyeJD071S20SBh4xoCCRH4PhqAWBijM9oXyhdZ6MM0t2JWegRo1iNJN5p0IhZDmLttr1SCHBvP1
kM3HbgpOjlQLU8B0JjkY8q1c9NLSbGynKTbf9Meh95QU8rIAB4mDH80zUIEG2qadxQ0191686FHn9Pi
read it and store it file say /tmp/keyfile
read it and store it in a variable $keyvariable
You just have to decide how much to read.
If this is the only input, you could read until end of file. This is how most UNIX utilities work:
#!/bin/bash
echo "Pipe in certificate, or paste and it ctrl-d when done"
keyvariable=$(cat)
If you want to continue reading things later in the script, you can read until you see a blank line:
#!/bin/bash
echo "Paste certificate and end with a blank line:"
keyvariable=$(sed '/^$/q')
If you want it to feel more like magic interactively, you could read until the script has gone two seconds without input:
#!/bin/bash
echo "Paste your certificate:"
IFS= read -d '' -n 1 keyvariable
while IFS= read -d '' -n 1 -t 2 c
do
keyvariable+=$c
done
echo "Thanks!"

Resources