0

The following code will output:

Line: 2023-06-29 14:57:45 (62.3 MB/s) - ‘www.bharatbenz.com/sales-enquiry/index.html?1015R+ - 10 ?? ?????? ?????? ???? ?? ?????  | ???????? ????-VI BS-VI ????.html’ saved [112171/112171]

Array: 2023-06-29 14:57:45 (62.3 MB/s) - ‘www.bharatbenz.com/sales-enquiry/index.html?1015R+ - 10 ?? ?????? ?????? ???? ?? ????? | uniq.txt ????-VI BS-VI ????.html’ saved [112171/112171]

the echo "Line:" $line outputs the correct string. But once you add it to the array and echo the array echo "Array:" ${savedUrlLines[@]} it has added uniq.txt into the string, which is a file in the local directory. If I move the script and run it in another directory then file names from that local directory will appear in the output, why?

Running on Ubuntu 20.04.5 LTS

Thanks

#!/bin/bash

savedUrlsFile=$(cat log.txt | awk '/saved/ {print $0}')

savedUrlLines=()

OLDIFS=$IFS
IFS=$'\n'
for line in $savedUrlsFile
do
  if [[ $(echo $line | grep 14:57:45) != '' ]]; then
    echo "Line:" $line
    savedUrlLines+=($line)
  fi

done
IFS=$OLDIFS

echo
echo "Array:" ${savedUrlLines[@]}
ddomen
  • 1
  • Does changing `echo $line` into `echo "$line"` help if you do it everywhere? You want string interpolation to happen within quotes. – Ted Lyngmo Jul 07 '23 at 17:25
  • 6
    [Shellcheck](https://www.shellcheck.net/) identifies several problems with the code. The report includes links to more information about the problems and how to fix them. It's a good idea to run [Shellcheck](https://www.shellcheck.net/) on all new and modified shell code. – pjh Jul 07 '23 at 17:27
  • Hi and welcome to StackOverflow! Clarification: Are the question marks you show literal question marks, or have you substituted them to obscure sensitive data? – Vercingatorix Jul 07 '23 at 17:57
  • This has nothing to do with arrays, and everything to do with wildcard expansion in unquoted variable references. [Double-quote your variable references](https://stackoverflow.com/questions/55023461/when-should-i-double-quote-a-parameter-expansion), and [don't try to read lines with `for`](http://mywiki.wooledge.org/DontReadLinesWithFor). In this case, it's a bit complicated because you're reading from a command's output rather than a file, *and* setting variables inside the loop, so you'd have to use something like `while IFS= read -r line; do ... done < <(cat ...)`. – Gordon Davisson Jul 07 '23 at 17:58
  • 1
    FWIW, the reason your two echos give different output is that you run them with different IFS values. When you echo the line by itself you have IFS set to only a newline so the content of `$line` doesn't split on spaces, but when you echo the array IFS has its original value so it DOES split on spaces, and then `????????` as a unique word expands to all eight-character filenames in your directory. Shellcheck will tell you to quote your variable expansions - this is the standard solution. – tjm3772 Jul 07 '23 at 17:59
  • 1
    @TedLyngmo Yes this solved the issue, thanks. I will need to research string interpolation to understand why. – ddomen Jul 07 '23 at 18:31
  • @pjh Thanks for the info on Shellcheck, will definitely use it in the future. – ddomen Jul 07 '23 at 18:31
  • @Vercingatorix The question marks are literal question marks. – ddomen Jul 07 '23 at 18:32
  • @GordonDavisson Thanks for the info, I'll read the links you provided. I had found the while solution before but don't understand the syntax enough to understand what it all means. Will keep looking into it. – ddomen Jul 07 '23 at 18:32
  • @tjm3772 thanks for the additional information. – ddomen Jul 07 '23 at 18:32
  • @ddomen See codeforester's answer to ["Looping through the content of a file in Bash"](https://stackoverflow.com/questions/1521462/looping-through-the-content-of-a-file-in-bash/41646525#41646525). BTW, I'd either skip the `awk` thing and do all the testing in bash with `if [[ "$line" = *saved* && "$line" != *14:57:45* ]]` or do *all* the line selection with `grep` and skip the loop: `readarray -t savedUrlLines < <(grep 'saved' log.txt | grep -v '14:57:45')` – Gordon Davisson Jul 07 '23 at 21:28

1 Answers1

0

As you are using awk, consider doing everything with it

#! /bin/bash

readarray savedUrlLines < <(awk '/saved/ && $2 == "14:57:45"' log.txt)
echo "Array:" ${savedUrlLines[@]}
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134