Escaping brackets in filename. Shell script [duplicate] - linux

This question already has answers here:
is it safe to use "ls" in for loop in bash
(3 answers)
When to wrap quotes around a shell variable?
(5 answers)
Closed 4 years ago.
I have many files with pattern name:
N.apple(d).log
e.g. 001.apple(d).log, 002.apple(d).log, etc.
The problem is when I am trying to do something with the files in a bash script, there is always an error 'No such file or directory' even with escaped brackets:
for i in $(ls my_folder); do file=$(echo $i | sed 's/(/\\(/g' | sed 's/)/\\)/g') ; head -1 my_folder/$file; done
Thanks for any tips

Use quotes. Also, don't use ls when the shell can expand the wildcards just fine already.
for i in my_folder/*; do head -1 "$i"; done

Related

Add markdown code tick to beginning and ending of a variable [duplicate]

This question already has an answer here:
How to escape backticks in bash
(1 answer)
Closed 3 months ago.
I searched all forums but didn't find a suitable way to achieve this.
variable
result=multiline
string and
other string
Want to convert it to
```result=multiline
string and
other string```
means add 3 ticks at the beginning and 3 ticks at the end of the file.
I know it's possible through sed, but I think it's not working because of special characters for example tick.
Any advice is appreciated.
Escaping backticks with \ helped.
echo $result > result.txt
echo "\`\`\`" | cat - result.txt > some-result.txt
echo "\`\`\`" >> markdown-result.txt

Replace hyphens with underscores in bash script [duplicate]

This question already has answers here:
Replacing some characters in a string with another character
(6 answers)
Difference between sh and Bash
(11 answers)
Closed 4 years ago.
Trying to write a bash script and in one part of it I need to take whatever parameter was passed to it and replace the hyphens with underscores if they exist.
Tried to do the following
#!/usr/bin/env bash
string=$1
string=${string//-/_}
echo $string;
It's telling me that this line string=${string//-/_} fails due to "Bad substitution" but it looks like it should do it? Am I missing something?
There is nothing wrong with your script, and it should work in modern versions of Bash.
But just in case you can simplify that to :
#!/bin/bash
echo "$1" | tr '-' '_'
This is in case that parameter substitution does not work ( which seems to be your case ).
Regards!

How to define $i in linux bash shell? [duplicate]

This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 4 years ago.
I am having a problem while using the bash shell. Here is my linux command code:
for i in `cat linshi`;do sed -i '/$i/d' a.txt;done
The content of linshi is:
aa
bb
The content of a.txt is:
aa:wwersdf12314231234
bb:weorpius2345234523
cc:ertoiu230498234098
dd:234092834asdfkdfkg
I want to delete the first and the second row of a.txt.
But unlucky, I found '/$i/d' is not correct. And I have tried '/\$i/d' and '/"\"$id/', but they are fail again. Who can help me?
Variables aren't expanded inside single quotes, only double quotes.
for i in `cat linshi`; do sed -i "/$i/d" a.txt; done
That said, you could do the same thing with:
grep -vf linshi a.txt
Instead of using single quotes use double quotes. '' doesn't undergo any variable expansion however double quotes do.
This will work:
for i in $(cat linshi);do sed -i "/$i/d" a.txt;done

How to delete last found value in Bash [duplicate]

This question already has answers here:
Bash : extracting part of a string
(4 answers)
Closed 6 years ago.
Say I have a string 0.0.25, how do I delete the last part after dot (including it) to make it like 0.0? Note that last part can have variable number of digits.
In bash you can do the following:
echo "${var%.*}"
See the Shell Parameter Expansion section of the manual.
Using awk you could:
echo "0.0.25" | awk -F. '{print $1"."$2}'

Replace a text with a variable [duplicate]

This question already has answers here:
sed substitution with Bash variables
(6 answers)
Closed 7 years ago.
How can I do this?
sed -i 's/wiki_host/$host_name/g' /root/bin/sync
It will replace wiki_host with the text $host_name.
But I want to replace it with the content of the variable..
I tried it with
sed -i 's/wiki_host/${host_name}/g' /root/bin/sync
It doesn't work either.
You need to use double quotes:
$ sed -i "s/wiki_host/${host_name}/g" /root/bin/sync
Your single quotes prevent the shell variable from being replaced with its contents.

Resources