Meaning of 2 in's in shell script for loop? [closed] - shell

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
What does the 2nd "in" do? I can't seem to find this kind of example anywhere other than this one right here.
for a in in /home/davidwright/attachments/*/*.tar
do
echo "extracting $x"
tar -xvf $x
done

Looks like a typo. It means the loop iterates with the string "in" as the first value assigned to a, then proceeds to iterate over the results of the glob. The shell's just not that picky, and every item after the first in is a thing to iterate over in the for loop. Unless there is a file named in that is known to exist, tar will complain when it tries to unpack the non-existent in file.

Related

replace strings with shell variables in text file [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
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.
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.
Improve this question
I'm writing a shell script in which I've created some variables.
RELEASE="something"
COMMONS="something else"
I've got a file file.txt in which there are some occurrences of $RELEASE and $COMMONS. I want to replace these strings with the corresponding shell variable. I've tried to run (and a lot of other variations):
sed "s|\$RELEASE|${RELEASE}|g" file.txt > result.txt
It replaces "$RELEASE" with "${RELEASE}." Have you any idea how to replace by the value of $RELEASE?
Try this :
sed 's|$RELEASE|'"$RELEASE"'|g' file.txt > result.txt

Delete word from phrase bash [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 6 years ago.
Improve this question
I would like to delete the word blob from phrase $CHANGEDSITE. but my code isn't working. Any suggestions?
#!/bin/bash
OLD=$1 ex. https://github.com/retep-mathwizard/imitate/blob/master/bjqx
NEW=raw.githubusercontent.com
CHANGEDSITE="${OLD/github.com/$NEW}"
REMOVEDBLOB="${CHANGESITE/blob/}"
echo $REMOVEDBLOB
Forgot a D in my variable, So CHANGESITE was nothing , hence the output being nothing
You can use sed
OLD=https://github.com/retep-mathwizard/imitate/blob/master/bjqx
echo $OLD | sed 's/github.com/raw\.githubusercontent\.com/g' | sed 's/blob\///g'
The First sed change the url and the second remove the word blob/

Bash case statement to create file [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 8 years ago.
Improve this question
Hello i am creating a simple case statement menu to implement an address book and i am having trouble.
the menu work fine but i am having trouble understanding why the file is not created through this command.
this is just primary testing to see if a file is created but is is giving me an operand error so i tried adding .txt to the end and it gave no feedback.
1) echo "Please enter a name for your addressbook"
read addName
touch $addname
break;;
read addName vs. $addname The names are not the same (uppercase n vs. lowercase n)
You could try this.
echo "Please enter a name for your addressbook"
read addName
touch $addName.txt
break;;
for a text file.

Set priority on variables in Bash [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 6 years ago.
Improve this question
I've few variables (the number of variables is dynamic) in csv format as below:-
var1,var2,var3...
I need to prioritize the variables in below manner
priority,variable
1,var1
2,var2
3,var3
......
and then compare the priority of variables using [IF Statement]
So how do I add priority/weight to each of the variables ?
While reading the lines of your file, use IFS and read -a to read the values into an array. Then, it's simple enough to iterate over the array and assign a priority based on the index.
echo "priority,variable"
while IFS=, read -a vars; do
for idx in "${!vars[#]}"; do
echo "$((idx+1)),${vars[idx]}"
done
done << END
a,b,c,d,e,f
END
priority,variable
1,a
2,b
3,c
4,d
5,e
6,f

Reading all files inside a folder with bash [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
The problem is to read all files inside a folder and apply certain functions specified in the bash script over each file. Somewhat like calling map() in an object in JavaScript, but in this case with bash.
you just need to loop over the files. Assume your pwd is that folder:
for file in *.txt; do
do your stuff here with "$file" ...
done

Resources