0

I have a file like this

/*

Created by Bla bla bla Copyright by XYZ December 20th, 2020

*/

"car" = "Carro"; "door" = "Porta";

I would like to read this file line by line and copy to another file.

So I have this bash script

file='file.txt'
output='output.txt'
touch $output

for oneLine in $(cat ${file}) do echo "$oneLine" >> $output done

when I open the output file this is what I see

/*

Created by Bla bla bla Copyright by XYZ December 20th, 2020

Project/ Files/ Images/ Sounds/

in other words, when the parsing arrives at the line containing the *\ it prints a list of directories that in fact are directories that exist at the same level of the input and the output files.

How do I solve that?

Duck
  • 4,674
  • You are in effect using the file contents as a pattern for for. Hence it expands i.e. */ to all files in current directory. Try echo */, or for f in */; do echo "# $f #"; done Not sure how you manage to get the whole lines from start of script. (First 6 lines). Suspect it's not your entire script. https://mywiki.wooledge.org/BashFAQ/001 - Wrote more in an answer but it got closed before I had a chance to post :P – ibuprofen Aug 17 '21 at 03:53
  • 1
    the unquoted $(cat ${file}) triggers word splitting and filename generation, and */ is a pattern that matches all directories in the current dir. See also: http://mywiki.wooledge.org/WordSplitting and Quoting within $(command substitution) in Bash (That /* should also already expand to some filenames, like /bin, /etc) – ilkkachu Aug 17 '21 at 08:25
  • 1
    Also Why is using a shell loop to process text considered bad practice? has a loop that works, and an explanation why you shouldn't do stuff line-by-line in the shell. You could get the result you're trying to get here more easily with just the single command cat -- "$file" > "$output". (Or if you're actually trying to do something other than just copy, with an appropriate sed or awk script.) – ilkkachu Aug 17 '21 at 08:26

0 Answers0