How to read multi-line input in a Bash script? [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 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!"

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.

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

Unix Shell scripting query on sed [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
Need a help on unix shell scripting. As I am new to this.
I have a shell script written. While running that script am giving argument as say 003. I need to replace this argument value in a particular line in shell scripts as below.
Script:
if [[ $# != 1 ]]
then
echo "Please enter the Value"
echo "eg: script.sh 003"
exit 0;
fi
Q=WMS.XXX.vinoth
I need to replace XXX value with 003 and append into a temp file. Can you please help me???
Thanks in advance!!!
Do you ask how to replace the XXX with first argument?
Q=WMS.$1.vinoth
Using the $1 will work, however be aware that you should probably use quotation marks when passing a number like 003 in stead of 3 as in some cases the two zeros in front might be dropped.
Also I recommend wrapping the string in quotation marks as well, avoiding accidental command calls.
./script "003"
if [[ $# != 1 ]]
then
echo "Please enter the Value"
echo "eg: script.sh 003"
exit 0;
fi
Q="WMS.$1.vinoth"

Clarification regarding bash shell 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
Can anyone explain the part where read i is used. Where did the i come from.
scp -i ~/.ssh/id_rsa.sample gaara#stuid.student.com:ready/$2/*.zip ./$2 > slate.out 2>&1
ls -1 $2/* > curr.lst 2>/dev/null
while
read i
do
if
test -e ../done/"$i"
then
diff "$i" ../done/"$i" >/dev/null 2>&1
if
test $? -eq 0
then
rm "$i"
fi
fi
done < curr.lst
This syntax for read is commonly used to process multiple lines from a file. Let's simplify things by omitting the inner loop:
while
read i
do
# Process i
done < curr.lst
The 'while x do; done' syntax is pretty basic and easily understood, but the addition of the I/O redirect can be confusing. When you add the < curr.lst after done, it means "use the contents of this file as stdin for the conditional. So, if you now omit the loop, you get:
read i < curr.lst
It is now clear that read is getting its input from curr.lst and setting the variable i to the contents of each line. So, what that block of code basically means is "process each line of curr.lst as the variable i with the code inside the loop.
"read" , according to man page (type man page in a shell), read from a file descriptor.
In your code, the loop is made for each row from "curr.lst", which are put in the variable $i

Resources