0

I have a file which was created by another scripts with file names. Only thing is that it contains * for similar file names. now i want to iterate a for loop on this file to execute a few operation but i want to do it on different servers for that i simply need exact same name from that file. For this, i used grep and passed it to 'for' loop for one by one execution. But instead before going for the loop it actually executes it on current server itself and gives wrong output. I don't know what i'm missing.

# node_name=auxiliary;grep -F $node_name /opt/log_automation/ME_log_sheet.csv|awk -F "," '{print $2}'
/opt/tpa/logs/install_SPS.*.log
/opt/tpa/logs/localization.log
/opt/tpa/logs/SPI.*.log
/opt/tpa/logs/TPA_system.log
#

This seems to be working as expected as i want * to be present in the name. Now if i put it in a variable or pass it to the loop it expands and gives actual present file which i don't want. I want it to return simply the name with * on it.

# node_name=auxiliary;myvar=$(grep -F $node_name /opt/log_automation/ME_log_sheet.csv|awk -F "," '{print $2}');echo $myvar
/opt/tpa/logs/install_SPS.2019.02.09-12:12.log /opt/tpa/logs/localization.log /opt/tpa/logs/SPI.2019.02.09-12:10.log /opt/tpa/logs/TPA_system.log

# node_name=auxiliary;for i in $(grep -F $node_name /opt/log_automation/ME_log_sheet.csv|awk -F "," '{print $2}');do echo $i;done
/opt/tpa/logs/install_SPS.2019.02.09-12:12.log
/opt/tpa/logs/localization.log
/opt/tpa/logs/SPI.2019.02.09-12:10.log
/opt/tpa/logs/TPA_system.log

Please help me resolve this. What am i doing wrong? P.S.: '-F' with grep i added after a few changes. It wasn't working either way.

Lucky Tyagi
  • 199
  • 1
  • 3
  • 12

2 Answers2

3

Quote all variables:

node_name=auxiliary
for i in "$(grep -F $node_name /opt/log_automation/ME_log_sheet.csv|awk -F "," '{print $2}')"
do
    echo "$i"
done
Poshi
  • 5,332
  • 3
  • 15
  • 32
0

I think you should be using set -f to disable filename expansion : https://unix.stackexchange.com/questions/333867/what-does-set-f-in-korn-shell-do

However as triplee suggest, using such characters (eg asterisk) in filenames is a very bad practice..

nullPointer
  • 4,419
  • 1
  • 15
  • 27
  • 1
    This is not a viable solution in the general case. You might as well suggest to never use asterisks (or spaces, or single quotes, or etc) in filenames. – tripleee Feb 18 '19 at 15:06