Bash: adding a key/value pair to an existing config file [duplicate] - bash

This question already has answers here:
How to append output to the end of a text file
(13 answers)
Closed 8 months ago.
I have an existing config config.ini file with the following content:
VALUE_A=a
VALUE_B=b
Using Bash, I'd like to add a new key-value pair VALUE_C=c to get the following:
VALUE_A=a
VALUE_B=b
VALUE_C=c
Is there a concise way to do this with Bash (ideally a one liner)?

As suggested in the comments, the answer is simply:
echo VALUE_C=c >> config.ini

Related

for loop in shell script now working as expected [duplicate]

This question already has answers here:
How to match nothing if a file name glob has no matches [duplicate]
(2 answers)
How to skip the for loop when there are no matching files?
(2 answers)
Closed 3 days ago.
I have some output files like output1.bin, output2.bin and I need to add them to the list output_files. It works as expected when output files exists, but it adds "output*.bin" in the list variable if output1.bin, output2.bin does not exists. I expected that if "${outputdir}"/output*.bin doesn't match any file it should not even iterate through it i.e. I expeted the list to be empty but it adds output*bin name to it. Can someone explain the behavior and the fix ?
for bin_file in "${outputdir}"/output*.bin; do
output_files+=("${bin_file}")
done

echo file names in reverse order [duplicate]

This question already has answers here:
How to reverse a list of words in a shell string?
(18 answers)
Sort 'ls' output by name
(13 answers)
Closed 7 months ago.
This post was edited and submitted for review 7 months ago and failed to reopen the post:
Duplicate This question has been answered, is not unique, and doesn’t differentiate itself from another question.
I would like to list files in reverse order, list of files listed are as below, the purpose is to echo all the file contents into another file.
vcp.status-200.txt
vcp-status-400.txt
vcp-status-500.txt
vcp-status-000.txt
I am currently running below command; echo will list all the files and xargs cat will append the content of these files into OutputFile.
echo *${USER}*status*.txt | xargs cat >> OutputFile
Current Output
vcp-status-000.txt vcp-status-200.txt vcp-status-400.txt vcp-status-500.txt
Intended Output
vcp-status-500.txt vcp-status-400.txt vcp-status-200.txt vcp-status-000.txt

How to iterate over pairs of files with corresponding names in Bash [duplicate]

This question already has answers here:
Looping over pairs of values in bash [duplicate]
(6 answers)
Closed 3 years ago.
I have a folder with test cases named like test_1_in, test_1_out,
test_2_in, test_2_out etc. I want to write a script, that can test my program with these pairs, I suppose it would do something like this in a loop
diff <($program < $test_in) <($test_out)
So the question is what is the best way to do it in Bash? How to iterate through the pairs of files? And also how to capture exception, that is print some error message if diff shows some difference?
Iterate over *_in, and generate *_out using parameter expansions:
for test_in in test_*_in; do
test_out="${test_in%_*}_out"
if diff <("$program" "$test_in") "$test_out"; then
echo "$test_in failed."
fi
done

Change multiple file names in a pattern [duplicate]

This question already has answers here:
Renaming files in bash
(5 answers)
Closed 7 years ago.
I have a number of files with somewhat similar names:
HappyBD_Stereo_144kbps.mp3
HappyBD_Stereo_192kbps.mp3
HappyBD_Stereo_256kbps.mp3
...
For some reason, I need to change/shorten these names into something like
HappyBD_Ste_144k.mp3
HappyBD_Ste_192k.mp3
HappyBD_Ste_256k.mp3
...
Can someone recommend a good way to automate this kind of file name changes? Thanks.
you can try,
for filename in `ls *_Stereo_*.mp3`; do
newfilename=$(sed 's/_Stereo_/_Ste_/g' <<< $filename);
mv $filename $newfilename;
done

In YAML, how do I make a comment over multiple lines [duplicate]

This question already has answers here:
How do you do block comments in YAML?
(11 answers)
Closed 6 years ago.
I know that you can make a single line comment in YAML by using the # tag, but I haven't been able to find something like /* in java that starts a comment & has to be finished off with a */. Does such an operator exist in YAML?
YAML does not support multiple line comments. If you want to use them. You can just try
# this
# is a multiple
# line comment

Resources