Nested substitution in shell script ($ within a $ field variable) [duplicate] - bash

This question already has answers here:
Bash - variable variables [duplicate]
(4 answers)
Dynamic variable names in Bash
(19 answers)
Closed 5 years ago.
I have a CSV file which contains the field names and values. The field names have given with a '#'
Eg.
test.csv
#field_1,field_2,field_3
1,axt,3
2,bss,3
I need to display the output like this:
heading_1=field_1
heading_2=field_2
heading_3=field_3
The script I have written so far:
headings=$(grep '^#' $arg_file)
echo "Fields name= $headings"
###### put a for loop to take in all headings as separate variables
for ((i=1; i<=$(echo "$headings"|tr ',' ' '|wc -w); i++))
do
echo "Trial $i field"
heading_$i=$(echo "$headings"|cut -d "," -f $i)
var="$(heading_$i)"
echo ${!var}
done
But this is giving me an error:
./script.sh: line 28: heading_1=#field_1: command not found
./script.sh: line 29: heading_1: command not found
What should I do to get the output the way I want? I am new to shell script and I am not really sure if that is even possible.

Related

Store Result of Bash Command in Shell Variable [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 4 months ago.
I am trying to store the result of a bash command within a for loop for use in a command. This is what I currently have:
for filename in /home/WIN/USER/files/*
var=$(basename ${filename%.*}) | awk -F'[_.]' '{print $1}'
do echo var
done
However, I am getting these errors:
./script.sh: line 2: syntax error near unexpected token `var=$(basename ${filename%.*})'
./script.sh: line 2: `var=$(basename ${filename%.*}) | awk -F'[_.]' '{print $1}''
Does anyone know how to fix this or how to do what I am trying to do?
Thanks.
Your for statement is wrong, and your variable assignment statement is also wrong. You shall write something like this:
for filename in /home/WIN/USER/files/*; do
var=$( your shell code goes here ) # you assign the output of the shell code here
echo "$var" # you echo the results here
done

Store into var the result of grep command shell script [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
How to pass the value of a variable to the standard input of a command?
(9 answers)
Closed 3 years ago.
I'am creating a shell script to extract a number from a particular line, where one particular string appears (isDone), for that i use a grep, i find the line and can echo it, but i can't store the grep output to a var.
$text var got inumerous tags, one of them having the string "isDone", thats the line i want:
code:
short_str="isDone"
echo "$text" | grep "$short_str"
output:
< s:key name="isDone">1</s:key >
now i want to store that output from grep into a file, and then extract the value (on this case is 1)
what have i tried:
store="$("$text" | grep "$short_str")"
echo "$store"
but that outputs all the file, what am i doing wrong?

Can't manage to give two arguments from a fil to bash script : command not found [duplicate]

This question already has answers here:
Why does a space in a variable assignment give an error in Bash? [duplicate]
(3 answers)
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 3 years ago.
I'm new to bash script, it is interesting, but somehow I'm struggling with everything.
I have a file, separated by tab "\t" with 2 infos : a string and a number.
I'd like to use both info on each line into a bash script in order to look into a file for those infos.
I'm not even there, I'm struggling to give the arguments from the two columns as two arguments for bash.
#/!bin/bash
FILE="${1}"
while read -r line
do
READ_ID_WH= "echo ${line} | cut -f 1"
POS_HOTSPOT= echo '${line} | cut -f 2'
echo "read id is : ${READ_ID_WH} with position ${POS_HOTSPOT}"
done < ${FILE}
and my file is :
ABCD\t1120
ABC\t1121
I'm launching my command with
./script.sh file_of_data.tsv
What I finally get is :
script.sh: line 8: echo ABCD 1120 | cut -f 1: command not found
I tried a lot of possibilities by browsing SO, and I can't make it to divide my line into two arguments to be used separately in my script :(
Hope you can help me :)
Best,
The quotes cause the shell to look for a command whose name is the string between the quotes.
Apparently you are looking for
while IFS=$'\t' read -r id hotspot; do
echo "read id is: $id with position $hotspot"
done <"$1"
You generally want to avoid capturing things into variables you only use once, but the syntax for that is
id=$(echo "$line" | cut -f1)
See also Correct Bash and shell script variable capitalization and When to wrap quotes around a shell variable?. You can never have whitespace on either side of the = assignment operator (or rather, incorrect whitespace changes the semantics to something you probably don't want).
You have a space after the equals sign on lines 5 and 6, so it thinks you are looking for an executable file named echo ABCD 1120 | cut -f 1 and asking to execute it while assigning the variable READ_ID_WH to the empty string, rather than assigning the string you want to the variable.

Bash "while read loop" does not properly recognize variables [duplicate]

This question already has answers here:
bash variable interpolation separate variables by a hyphen or underscore
(3 answers)
Error in string Concatenation in Shell Scripting
(3 answers)
Closed 5 years ago.
I am using MacBook pro terminal to execute a shell script. It loops through a text file and create filenames based on each line in the file.
#!/bin/bash
year=2010
list=list_test.txt
mydir=thisdir
i=1 # counter
while read line
do
echo $i $line
file1=`echo $mydir/file_$year_$line_test.tif`
file2=`echo $mydir/file_$year_$line_test.tif`
echo $file1 $file2
i=$(($i+1))
done < $list
However, the output is peculiar:
1 17019
thisdir/file_.tif thisdir/file_.tif
2 17029
thisdir/file_.tif thisdir/file_.tif
3 17039
thisdir/file_.tif thisdir/file_.tif
Within the loop, bash does not recognize some variables, like "year" which is a global, and "line" which is read from the text file. The text file is as below:
17019
17029
17039
Another script with exactly the same manner works very well. This is mysterious to me now.
Any help or comments are extremely appreciated! Thanks very much!
_ is a valid character for an identifier, but you want to use it as a literal character in the file name. You need to use the full form of parameter expansion, ${x} instead of $x.
(Also, the command substitution isn't necessary.)
file1=$mydir/file_${year}_${line}_test.tif
file2=$mydir/file_${year}_${line}_test.tif

Variable set inside while loop is not visible outside [duplicate]

This question already has answers here:
A variable modified inside a while loop is not remembered
(8 answers)
Closed 4 years ago.
Variable $adsQ is an output of sql. This variable has number of lines in matrix format. When I pass this variable with pipe to while loop, it works fine whereas I loose variable required from while loop. As suggested from other forums, I modified with followings
varout=''
while IFS= read -r adrow;
do
<... do something....>
varout="$varout $adrow"
done < <(printf '%s\n' $adsQ)
echo "output of while $varout"
echo "AFTER adsVal >> $adsVal"
when I run this, i get error stating
test.sh: line 72: syntax error near unexpected token <'
test.sh: line 72: done < <(printf '%s\n' $adsQ)'
You have an extraneous < in your command. You wrote
done < <(...)
Remove the first <.

Resources