简体   繁体   中英

How to put variables in linux command?

I want to know how to put variables in linux command, here is my code:


while read line
do
$site=$line
# execute extract command foreach line(each line contain a site name)
../../data/PROG/launch extractdata -hdf 2013-01-01,2014-01-01 -site  "$site"  >data/hour_$site_file.txt
 done < data/extract_site.txt

i explain:

this file "data/extract_site.txt" contains a list of sites name, each line is a site name, and for each site i want to extract data and put the result of the commande in a file named "hour_$site_file.txt". For example i have a site name "OPE", the command would be: ../../data/PROG/launch extractdata -hdf 2013-01-01,2014-01-01 -site OPE and the file name will be called: hour_OPE_file.txt

when i execute that i have error: undefined command "=OPE"

Iam a beginner in shell, any help?

Thanks in advance.

PS: sorry for my english, i'm french.

Variable name as L-value does not need $.

Variable names chained should be enclosed into brackets as in hour_${site}_file.txt

while read line
do
site=$line
# execute extract command foreach line(each line contain a site name)
../../data/PROG/launch extractdata -hdf 2013-01-01,2014-01-01 -site  "$site"  >data/hour_${site}_file.txt
 done < data/extract_site.txt

Use braces to separate parameter names and use quotes to prevent word splitting:

while read -r line; do
    site=$line
    ../../data/PROG/launch extractdata -hdf 2013-01-01,2014-01-01 -site "$site" > "data/hour_${site}_file.txt"
done
  • When assigning a value to a variable, don't add a leading $ to the target: $site=$line was wrong.
  • -r optionally prevents backslashes from input to be interpreted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM