Clarification regarding bash shell 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
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

Related

concat strings of file cat in bash script [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 2 years ago.
Improve this question
I'm modifying this recursive directory crawling script from https://unix.stackexchange.com/questions/494143/recursive-shell-script-to-list-files
my plan is just to concatenate strings of the output of text files in my directory
so like: content+= content + cat file.txt, I feel like what I have isn't too far off but right now the output is blank and I guess I'm misunderstanding how to separate the cat function from the string concat portion
I tried using less instead of cat but that produces the same result I'm following the link below but none of the examples seem to produce an output for me
How to concatenate string variables in Bash
#! /bin/bash
echo "starting script"
content="start =>"
walk_dir () {
shopt -s nullglob dotglob
for pathname in "$1"/*; do
if [ -d "$pathname" ]; then
walk_dir "$pathname"
else
case "$pathname" in
*.txt|*.doc)
#printf '%s\n' "$pathname"
content+=(cat "$pathname") #add file contents to the previous string
esac
fi
done
echo "$content"
}
DIR=/c/Users/xxxx/Desktop/xxxx
walk_dir "$DIR"
pretty new to bash scripting but
Had a quick look - I think you are missing the $ sign in concat.
content+=$(cat "$pathname")
Alternatively, you can use backquote instead of parentheses without $.
You will need to say content+="$(cat "$pathname")" to append the
output of cat to the scalar variable content.
Or you can just say:
shopt -s extglob globstar
content="$(cat **/*#(.txt|.doc))"
echo "$content"
to concatenate the contents of the text/document files recursively.

Write a script to get the line number of a file with "while read LINE", but failed [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
#!/bin/sh
num=1
cat $1 | while read LINE
do
num=`expr $num + 1`
done
echo $num
Your script is spawning a sub-shell when you use pipe after useless cat. All the changes to $num made inside sub-shell get lost after while loop ends and you get back to parent shell.
You should initialize num with 0 not 1
It is better to not to use all capital letter variable names to avoid collision with internal shell variables.
Instead of reverse tick you should use $(...) for command substitution.
You should use:
#!/bin/sh
num=0
while read -r line
do
num=$(expr $num + 1)
done < "$1"
echo $num

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"

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