(bash) how to add some iteraion to filename - linux

I have the script that creates some .html and .txt files every day. But now it is only one file html and txt with changed content, I need every day a new html&txt file with date oof creation in the file name like : index_22-05-2013.html , i have these variables in shell script:
DESTINATION_HTML="./daily/html/index_$(date +"%F").html"
DESTINATION_TXT="./daily/txt/index_$(date +"%F").txt"
and a line in shell script that running one python script and creates html file
python `somescript.py` -m ${FILELIST[0]} ${FILELIST[1]} > $DESTINATION_HTML
and i`m getting this file created:
index_$(date +"%F").html
what i must to do to get this file name : index_22-05-2013.html

Sorry, I am not following you, but since
echo "index_$(date +%F).html"
outputs index_2013-08-20.html instead of index_22-05-2013.html which is what you need, you probably want to use this command instead:
echo "index_$(date +%d-%m-%Y).html"
Hope it helps! :)

Related

write all statements that user execute it during a session

I have this question.
(Write "your_username.sh" script to create a text file each time you log out. The file should
contain all statements you execute during your session. The name of the text file should have the following naming pattern “Statements-20191104.0225.txt” (20191104
represents the date and 0225 represents the time). All files should be stored in the
~/MyStatements directory.)
I create sh script and call it inside .bash_logout.
This script creates a file and tries to save all history inside it
#!/bin/bash
currentDateTime=$(date +"%Y%m%d.%k%M")
fileName="Statements-$currentDateTime"
touch ~/MyStatements/$fileName
echo $currentDateTime
echo $fileName
history -a "~/MyStatements/$fileName"
history -a newFile.text
A new file created inside "MyStatements" folder but this file doesn't contain any data
Try to redirect your output:
history -a > ~/MyStatements/$fileName
history -a > newFile.text
See Redirect all output to file

Obtaining file names from directory in Bash

I am trying to create a zsh script to test my project. The teacher supplied us with some input files and expected output files. I need to diff the output files from myExecutable with the expected output files.
Question: Does $iF contain a string in the following code or some kind of bash reference to the file?
#!/bin/bash
inputFiles=~/project/tests/input/*
outputFiles=~/project/tests/output
for iF in $inputFiles
do
./myExecutable $iF > $outputFiles/$iF.out
done
Note:
Any tips in fulfilling my objectives would be nice. I am new to shell scripting and I am using the following websites to quickly write the script (since I have to focus on the project development and not wasting time on extra stuff):
Grammar for bash language
Begginer guide for bash
As your code is, $iF contains full path of file as a string.
N.B: Don't use for iF in $inputFiles
use for iF in ~/project/tests/input/* instead. Otherwise your code will fail if path contains spaces or newlines.
If you need to diff the files you can do another for loop on your output files. Grab just the file name with the basename command and then put that all together in a diff and output to a ".diff" file using the ">" operator to redirect standard out.
Then diff each one with the expected file, something like:
expectedOutput=~/<some path here>
diffFiles=~/<some path>
for oF in ~/project/tests/output/* ; do
file=`basename ${oF}`
diff $oF "${expectedOutput}/${file}" > "${diffFiles}/${file}.diff"
done

Linux script that reads a input file

I would like to seek for your help on how to write a simple bash script that reads an input file (e.g. txt file) that has a list into it (e.g. numbers/letters), process it and use that list to delete single/multiple lines on a different txt file. I tried using sed with no luck.
Hope someone can help me...
Thanks in advance and more power!
To read a file, you can use the following code:
#!/bin/bash
file="$1"
while read line
do
#process each line
done < file

How to concatenate a string from an included file in bash

What I'm trying to accomplish is having a central configuration file, in bash, that defines some variables that are re-used in different bash files. The example below attempts to generate a file name with the current date included in the file name as well as a variable defined in another shell script. However whenever I try to concatenate this external variable it doesn't work. I can concatenate the variable in any other situation.
Example Code:
../config/vars.sh
#!/bin/bash
mysqlUser="backupuser"
mysqlPwd="fakePwd"
mysqlSocket="/var/run/mysqld/mysqld.sock"
mysqlPort="3306"
serverName="s01.catchyservername.com"
./dbBackup.sh
#!/bin/bash
source ../config/vars.sh
tempName=$(date +"%Y%m%d.sql.gz")
fileName="mysqld_${mysqlPort}_${tempName}"
echo "mysqld_${mysqlPort}"
echo ${tempName}
echo ${fileName}
output of dbBackup.sh
mysqld_3306
20140926.sql.gz
_20140926.sql.gz
As you can see when echoing "mysqld_${mysqlPort}" I get the expected output, but when echoing ${fileName} the entire first half of the string is ignored. What am I misunderstanding?
Your vars.sh file was probably created with a DOS/windows text editor:
$ ./dbBackup.sh
mysqld_3306
20140926.sql.gz
_20140926.sql.gz
$ dos2unix vars.sh
dos2unix: converting file vars.sh to Unix format ...
$
$ ./dbBackup.sh
mysqld_3306
20140926.sql.gz
mysqld_3306_20140926.sql.gz
$
As you can see above, I use the dos2unix utility to convert the line separators to Unix style.

Reading data from properties file in shell script

I am writing a shell script which reads data from a properties file and stores in into a local variable in shell script. The problem is when i am trying to read multiple properties from the file and form a string its getting over written
#!/bin/bash
. /opt/oracle/scripts/user.properties
echo $username
echo $password
echo $service_name
conn=$username$password$service_name
echo $conn
the values of username=xxxx password=yyyy and service_name=zzzz i expect the output to be
xxxxyyyyzzzz
but instead of that i am getting the output as
zzzz
please tell me where am i doing the mistake ?
I'm certain that the file /opt/oracle/scripts/user.properties contains CR+LF line endings. (Running the file command for the properties file would say ... with CRLF line terminators). Changing those to LF using dos2unix or any other utility should make it work.
Moreover, instead of saying:
conn=$username$password$service_name
you could say:
conn="${username}${password}${service_name}"

Resources