King David King David - 10 months ago 55
Linux Question

parameters with special characters in bash script

We have the following CSV file,

file include parameter and value

more hadoop.txt

security.admin.protocol.acl,*
security.client.protocol.acl,***
xasecure.add-hadoop-authorization,true
ranger.plugin.hdfs.policy.rest.ssl.config.file,/etc/hadoop/conf/ranger-policymgr-ssl.xml,
ranger.plugin.hdfs.policy.source.impl,org.apache.ranger.admin.client.RangerAdminRESTClient
ranger.plugin.hdfs.policy.cache.dir,/etc/ranger/{{repo_name}}/policycache",
ranger.plugin.hdfs.policy.pollIntervalMs,@$!!
ranger.plugin.hdfs.service.name,{{repo_name}}


the following code is part of my bash script:

while read -r line
do

param=` echo $line | awk -F ',' '{print $1}' `
val=` echo $line | awk -F ',' '{print $2}' `
echo $val
if [[ $val = $hadoop_val ]]
them

done < $hadoop.txt


the problem is when val is "*" or "@$!!" or other special characters then we get different value from:

echo $val


how to improve the code in order to get the original values as "*" or "@$!!" from echo $val

Answer Source

To prevent special characters to be expanded, you can enclose your variable between double quotes. As an aside you don't have to use awk, just change the IFS:

IFS=,
while read -r param val 
do
    echo "$val"
done < doop.log
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download