Generating arrays from command response with Bash [closed] - bash

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")

Related

Why won't a value in this bash script equal a command? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
When I run this code, the variable "thing" doesn't change its value to the command. I've tried everything and I just can't get it to work. I want thing to equal something like "1 history cd /bin
history cd /home/user/"
#!/bin/bash
val="thing"
function send () {
thing
thing=$(history | tail -n 2)
echo $thing
echo $val
# echo $last
if [ "$val" == *"this"* ]; then
echo "yes"
fi
exit 1
}
send
If you wonder why $(history | tail -n 2) returns nothing, it is because history lists commands previously ran in the current shell.
But your script is a new shell instance, so it does not carry the history of commands you ran before you execute your script.
If you want that, you have to source the script, not execute it. To source, do:
$ . thescript.bash
instead of
$ ./thescripts.bash
instead of this also
$ bash thescript.bash
Note: put your code in https://www.shellcheck.net/ to see syntax issues.

Store output of a command in a variable [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
In my mac terminal I am trying to store the output of a command into a variable like this
$ animals=$(cat animals.txt | head -n $plusOne | tail -n $numberofanimals | sort -u | sed -E "s/[*#/#2]//g; s/D/d/g ; s/G/g/g ; s/E/e/g ; s/H/h/g ; s/O/o/g ; s/C/c/g ; s/[!]//gā€)
>
but it does not work. When I press Enter I get a > prompt on the next line. Any idea why?
You've got a smart quote ā€ at the end of the line instead of a regular double quote ".

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

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!"

Combining loops and file redirection [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 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"

Resources