update variable string value in bash script [closed] - linux

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 months ago.
Improve this question
I need to update the value of a variable in a bash script and write the changes to the file. My attempts have either replace the key without strings or deleted the value.
Here's what ive tried :
sed -ri 's|^(VARIABLE_KEY\s*=\s*).*|\new_java-microservice.jar|' path_to_file/bashscript.sh
sed -i "s#\(.*VARIABLE_KEY =\)\( .*\)#\1 "new_java-microservice.jar"#" path_to_file/bashscript.sh
sed -i 's/VARIABLE_KEY =.*/VARIABLE_KEY ="new_java-microservice.jar"/' path_to_file/bashscript.sh
script
#!/bin/bash
COMMAND="$1"
HOME="/root/service/"
BASE="${HOME}/javaservice"
VARIABLE_KEY="existing-java.jar"
case "${COMMAND}" in
"start")
nohup java -jar ${BASE}/${VARIABLE_KEY} start > /dev/null 2>&1 &
;;
"stop")
#....
;;
*)
echo "Usage: $0 {start|stop}"
;;
esac

sed -i 's/^VARIABLE_KEY=.*/VARIABLE_KEY="new_java-microservice.jar"/' path_to_file/bashscript.sh

Related

paste data based on certain condition [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 1 year ago.
Improve this question
experts i have many files in a directory and these files contain some numerical numbers.
Probably not POSIX-compatible; tested on Bash 5.0. Assuming that the filenames don't contain whitespaces and have fixed length.
for file_with_a in *A.??
do
target_left=${file_with_a:0:15} # e.g., 2019__01_NDV.NT
target_right=${file_with_a:16:3} # e.g., .AS
cat $target_left[A-C]$target_right > ${target_left}_ABC$target_right
done
With mapfile aka readarray which is a bash4+ feature.
#!/usr/bin/env bash
files=([0-9][0-9][0-9][0-9]__*[ABC]*.??)
while mapfile -d '' -n3 array && ((${#array[*]} == 3)); do
if [[ ${array[0]%%_*} == ${array[1]%%_*} && ${array[0]%%_*} == ${array[2]%%_*} ]]; then
paste "${array[0]}" "${array[1]}" "${array[2]}" > "${array[0]%${array[0]:(-4)}}_ABC.${array[0]:(-2)}"
fi
done < <(printf '%s\0' "${files[#]}")

Parsing a conf file in 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
Here's my config file
#comment 1
--longoption1
#comment 2
--longoption2
#comment 3
-s
#comment 4
--longoption4
I want to write a bash script that will read this .conf file, skip comments and serialize the commandline options like so.
./binary --longoption1 --longoption2 -s --longoption4
Working off of this post on sed, you just need to pipe the output from sed to xargs:
sed -e 's/#.*$//' -e '/^$/d' inputFile | xargs ./binary
As Wiimm points out, xargs can be finicky with a lot of arguments and it might split it up across multiple calls to binary. It may be better off to use sed directly:
./binary $(sed -e 's/#.*$//' -e '/^$/d' inputFile)

A bash loop to echo all possible ASCII characters [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 4 years ago.
Improve this question
I know how to print all letters
{a..z} and {A..Z} and {0..9}
But is there a way to print all possible ASCII Characters via a bash loop?
You don't need a loop
echo -e \\x{0..7}{{0..9},{A..F}}
It prints all chars from 0 to 127.
If it is okay to use awk:
awk 'BEGIN{for (i=32;i<127;i++) printf("%c", i)}'
Or using printf:
for((i=32;i<127;i++)) do printf "\x$(printf %x $i)"; done
use this:
for ((i=32;i<127;i++)) do printf "\\$(printf %03o "$i")"; done;printf "\n"

bash printf not working - decimals [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 4 years ago.
Improve this question
Why did printf didn't WORK for the final command printf "%s,%.2f\n" "$s","$a" and what's that extra 0.00 coming from?
When I ran them individually, they worked as expected but not in the final command.
$ s="giga,fifa"; a="8309.18694444444444444444"; echo "$s"; printf "%s\n" "$s"; echo -e "\n"; echo "$a"; printf "%.2f\n" "$a"; echo -e "\n"; echo "$s,$a"; printf "%s" "$s,"; printf "%.2f\n" "$a";echo;printf "%s,%.2f\n" "$s","$a"
giga,fifa
giga,fifa
8309.18694444444444444444
8309.19
giga,fifa,8309.18694444444444444444
giga,fifa,8309.19
giga,fifa,8309.18694444444444444444,0.00
How can I get this output: giga,fifa,8309.19 with just one printf command showing both variables?
You don't use a , in bash printf, you delimit with space. The 0.00 comes from trying to parse the "$s","$a" at once, and has odd results - everything is considered one argument and printed as the first string, so no argument exists for the second and a 0 is substituted as default. This works as expected:
>printf "%s,%.2f\n" "$s" "$a"
giga,fifa,8309.19

Rename large number of files in bash [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 have a large list of files that I want to rename.
Much like this
So this is what my files look like
something.pcap1
something.pcap10
something.pcap11
something.pcap12
...
something.pcap111
something.pcap1111
essentially I want to rename all of the files so that the numbers get padded with 0's and they are 5 digit numbers.
something.pcap00001
A simple for loop should do the trick (can be script file):
for file in $(ls -1 something.pcap*); do
[[ ${file} =~ ^something.pcap([[:digit:]]*).* ]]
newfile=$(printf "something.pcap%05d" ${BASH_REMATCH[1]})
mv ${file} ${newfile}
done
Something like this?
rename 's/\d+$/sprintf("%05d",$&)/e' soemthing.pcap*
Note: this works with the rename as found in debian and its derivates.
What about something like this?
#!/bin/bash
for i in $(ls something.pcap*); do
q=$(echo $i|sed -e 's/pcap/pcap00000/;s/pcap0*\([0-9]\{6,\}\)$/pcap\1/')
mv $i $q
done
I hope this will help

Resources