ccyymmddhhmmss format for shell script [duplicate] - linux

This question already has an answer here:
How to convert any date to YYYYMMDDHHMMSS using unix shell script?
(1 answer)
Closed 1 year ago.
How to create text files with date and time as per below format:
vendor_brandname_ccyymmddhhmmss.txt
currently I am using below format:
DATE=date +%Y%m%d
file1=HospTOHome_BRC_$DATE.txt

The + argument isn't limited to just placeholders.
$ date +"HsopTOHome_BRC_%Y%m%d%H%M%S.txt"
HsopTOHome_BRC_20210630082847.txt
You can set the value of file1 with a command substitution:
$ file1=$(date ...)
To actually create the file, you can use touch:
$ touch "$file1"
or redirection the output of some command that generates the contents you want:
$ ls > "$file"
or pass it as the argument to your favorite text editor:
$ vi "$file"

Related

how to copy the bash file itself to the output dir [duplicate]

This question already has answers here:
How do I know the script file name in a Bash script?
(25 answers)
Closed 2 years ago.
I want to save a copy of the bash file each time I run it. It should be saved to the output dictionary.
I am doing it like this in the mytrainrtest.sh file:
mkdir -p "${EXP_DIR}/train"
cp "${WORK_DIR}"/mytrainrtest.sh "${EXP_DIR}"/.
Now I have much more bash files with name my****** as copies of the upper one, each with different names.
How can I write the line, so the bash file will recognize its name to copy itself?
Use the $0 special variable which contains the name of the currently executing script.
cp "$0" "$exp_dir"/
Script name(path) stored in a special var $0
#!/bin/bash
echo $0
$ ./test
./test

How to format linux mpstat output in multiple lines [duplicate]

This question already has answers here:
Capturing multiple line output into a Bash variable
(7 answers)
Closed 3 years ago.
I have a small script where I appended the output of linux mpstat to a log file.
#/bin/bash
CPU_USAGE=$(mpstat)
echo $CPU_USAGE >> temp.log
The problem is that the output of mpstat on the terminal is formatted properly in 3 lines like so
However, the output to the file is all in one line.
How do I format the output like the one on the terminal?
Just quote the variable so it is not seen as several different parameters to be printed one after the other:
echo "$CPU_USAGE" >> temp.log
You could just directly pipe the output to the file:
#!/bin/bash
mpstat >> temp.log
If you must store it in a variable, then quote it like:
#!/bin/bash
CPU_USAGE=$(mpstat)
echo "$CPU_USAGE" >> temp.log
Otherwise, bash will not interpret the newlines as part of the message to echo, but the whole output as a list of short strings to output.

How to source a key value paired file in bash escaping whitespace? [duplicate]

This question already has answers here:
Use key/value data from a file in a shell script
(1 answer)
Reading key/value parameters from a file into a shell script
(1 answer)
Closed 3 years ago.
$ cat foo.txt
a=1one
b=2two
c=3 three
d=4four
$ source foo.txt
bash: three: command not found...
Need to set all the variable listed in foo.txt, how to source this file by escaping the space character? foo.txt comes from other application, which I cannot control, or is there an alternative to source ?
If the output is so regular, you could try to preprocess the file using sed like this:
$ sed -e "s/=/='/;s/$/'/" < foo.txt >sourced.env
and then source sourced.env. This will add a ' just after the = and add an ending '.

How do I add time to a date in unix shell script? [duplicate]

This question already has answers here:
Date arithmetic in Unix shell scripts
(14 answers)
How do I do date math in a bash script on OS X Leopard?
(8 answers)
Calculating time (adding minutes) bash
(1 answer)
Subtract days from a date in Bash
(7 answers)
Closed 4 years ago.
I have a file that contains date values. I want to be able to pull the last line of the file, formatted like "'2018-09-18 16:42:57'" add 1 day to it and store that into a variable. The code I have right now looks like the following, but it does not work:
start_date=$(tail -n 1 run_dates.txt)
start_date=$(start_date -d "+1 day")
What is the correct syntax to do this?
You can use this one-liner gnu date command to extract last line of the file, add one day and store output in a variable:
start_date=$(date -d "$(tail -n 1 run_dates.txt) +1 day" '+%Y-%m-%d %T')
To check variable content use:
declare -p start_date
declare -- s="2018-09-19 11:42:57"

Concatenating multiple text files by arguments in a script into a single file in Bash [duplicate]

This question already has answers here:
How to cat multiple files from a list of files in Bash?
(6 answers)
Closed 4 years ago.
How should i concatenate multiple text files get by arguments in terminal using a script in Bash?
#!/bin/bash
while read $1
do
cat $1 > cat.txt
done
I tried that example but it is not working.
You should use '>>' to concatenate ('>' will create a new file each time):
for file in "$#"
do
cat $file >> result
done

Resources