Issue
I wrote a script which takes two input: first is the txt file to read, the second is a value to search in this file:
Input file 'vs.txt':
4ZAF:CTG trilex A-DNA:0.9:F. Haws, A. Tee, Z. Jon, M. Hams:2012-05-10
9CAA:Structure of a B-DNA dodecamer:1.9:T. Takano, C. Broka, S. Tanaka, K. Itakura:1934-08-31
7SBD:Structure analysis of the B-DNA hexamer:0.8:T. Chass:2018-09-15
5SID:Resolution crystal of A-DNA in complex:1.2:P. Drassdzal, M. Gal, R. Kisek, L. Lomdek:2009-01-03
my script 'structure' is:
#!/bin/bash
read $1 $2 -n 2
sed -e 's/,/\s/g' $1 > vstup1.txt
read -p "Zadejte desetinne cislo: "
while IFS=":" read struktura nazev rozliseni autori rok ; do
if [[ $struktura = $2 ]] ; then
echo "struktura =" $struktura
echo "nazev =" $nazev
echo "rozliseni =" $rozliseni+$REPLY=$(awk "BEGIN {print $rozliseni+$REPLY; exit}")
echo "autori =" $(echo "$autori" | tr -s '[:lower:]' '[:upper:]')
year="${rok:0:4}"; echo "rok =" "${year}"
fi
done < vstup1.txt
if I run it like:
structure vs.txt 4ZAF
I see the bag:
./structure: line 2: read: `vs.txt': not a valid identifier
Even though the script is running ok on my Ubuntu 18.04
Solution
Here's what your script should look like:
#!/bin/bash
read -p "Zadejte desetinne cislo: " cislo
while IFS=":" read struktura nazev rozliseni autori rok ; do
if [[ $struktura = $2 ]] ; then
sum=$(awk "BEGIN {print $rozliseni+$cislo; exit}")
echo "struktura =" $struktura
echo "nazev =" $nazev
echo "rozliseni =" $rozliseni + $cislo "=" $sum
echo "autori =" $(echo "$autori" | tr -s '[:lower:]' '[:upper:]')
year="${rok:0:4}"; echo "rok =" "${year}"
fi
done <"$1
I've tested with the input you've provided and it works.
Answered By - Nik S Answer Checked By - Gilberto Lyons (WPSolving Admin)