0

I am trying to output stored variable as a file name. However only one variable is displayed in the file name. $1 is "HH" on the command line and filecount is equal to 13. The output file name is 2019-07-24-16:44:56.csv. I can not get the "HH" in the beginning of the file name. I would like to see a file as sure HH_2019-07-24-16:44:56-13.csv. I am missing something?

#count the number of html email files in current directory
filecount=$(ls -1 *.html | grep -v '/$' | wc -l)
#Current date file is created
filedate=$(date +%F-%T)
filename="$jobtype$filedate-$filecount.csv"
echo $jobtype >> $filename
echo $filecount >> $filename
echo $filedate >> $filename
Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
  • #count the number of html email files in current directory filecount=$(ls -1 *.html | grep -v '/$' | wc -l) #Current date file is created filedate=$(date +%F-%T) filename="$jobtype$filedate-$filecount.csv" echo $jobtype >> $filename echo $filecount >> $filename echo $filedate >> $filename – sillyrabiit Jul 24 '19 at 20:57
  • @DaltonCézane, this probably goes for your question as formatted code block. Can you edit your question and remove your comment? – Léa Gris Jul 24 '19 at 21:01
  • 2
    Please read [BashFAQ/004](https://mywiki.wooledge.org/BashFAQ/004) and [don't parse `ls`](https://mywiki.wooledge.org/ParsingLs). I don't see you using `$1` in your code. You may need to do `jobtype=$1`. Also, [quote your variables](https://mywiki.wooledge.org/Quotes). – Dennis Williamson Jul 24 '19 at 22:40
  • BTW, every time you run a command with `>> $filename` on the end, it opens the file at the start of that command and closes it after it. Much more efficient to open it once and hold it open; `exec 3>"$filename"`, for example, and then anything you send `>&3` will be appended there without needing to reopen the file first. – Charles Duffy Jul 25 '19 at 00:21
  • ...another note -- this code currently has race conditions; two versions can get the same `filecount` if they both are running `ls` at the same time and neither one actually got to where it's creating the new file yet. That's not a theoretical concern -- it causes people real-world problems; see f/e https://stackoverflow.com/questions/30332137/xvfb-run-unreliable-when-multiple-instances-invoked-in-parallel – Charles Duffy Jul 25 '19 at 00:22
  • ...the immediate impetus for the question, though, just looks like a missing `jobtype=$1`. – Charles Duffy Jul 25 '19 at 00:24

0 Answers0