1

I'm having an issue getting the variables $myname, $filename, and a method that counts all the records in another file, wc -l < hs_alt_HuRef_chr10.fa >> "$CuestaP.txt, to append to the text file CuestaP.txt. Code is below I don't see why they aren't appending.

myname="Pablo Andres Cuesta"    #creates variable containing my name

echo "Hello my name is:"    
echo "$myname"            #Display myname
echo
echo "This program is called:"    
filename=$(basename "$0")    #Display name of file without ./
echo "$filename"

echo

echo "$myname" >> "$CuestaP.txt" #Puts myname inside text file
echo "$filename" >> "$CuestaP.txt" # Puts file name inside text file

echo "The number of records inside the file 'hs_alt_HuRef_chr10' are:"
wc -l < hs_alt_HuRef_chr10.fa
wc -l < hs_alt_HuRef_chr10.fa >> "$CuestaP.txt" #Put amount of records inside file



echo "$USER" >> "$CuestaP.txt" #Add username to text file
echo "$PWD" >> "$CuestaP.txt" #Add file location to text file


if [ -s *.fa ]; then
    # read the age from the file.
    # if the file exists and is not empty, see flags above
    echo "my *.fa file exists and has data in it" >> 
"$CuestaP.txt" 

else
echo "THIS DID NOT WORK CORRECTLY" >> "$CuestaP.txt" 

fi

echo
cat CuestaP.txt

My output: Hello my name is: Pablo Andres Cuesta

This program is called:
CuestaPOGpgm3.sh   

The number of records inside the file 'hs_alt_HuRef_chr10' are:
1842651

#myname is missing
#filename is missing
#my *.fa file exists and has data in it is missing
pcuesta    #my username went through though?
/home/pcuesta    #my pwd went through though?
1
  • Did you check whether the variable CuestaP contains the correct value? Perhaps the strings get appended to a different file than you expect. Do a set -x before appending the strings. Commented Mar 26, 2018 at 6:27

2 Answers 2

0

The problem is the file name $CuestaP.txt. Either you want $ to be literally part of the file name then you need single quotes or a backslash: '$CuestaP.txt', \$CuestaP.txt. Or this is supposed to be a variable but then you both forgot to define it and mixed it with a literal.

Your data is in the file .txt

2
  • I do not want $ to literally be part of the file name, this is suppose to be a variable, I thought to append things to a text file this was the correct syntax: echo "$myname" >> "$CuestaP.txt" How come this works for $USER and not the other variables I created? Commented Mar 25, 2018 at 20:34
  • @PabloCuesta You need something like logfile=CuestaP.txt and then you can use ... >>"$logfile". I seriously doubt that this works with $USER. You must be more precise anyway. Commented Mar 25, 2018 at 20:41
0

[ -s *.fa ] will fail if there are multiple files that have filenames that end in .fa. You either test it with a single filename (hs_alt_HuRef_chr10.fa?) or you loop:

for name in *.fa; do
    if [ -s "$name" ]; then
        printf '"%s" has data in it\n' "$name"
    fi
done >>"$CuestaP.txt"

This, along with much of the rest of the output statements, assumes that CuestaP is a variable. $CuestaP.txt will expand to the contents of that variable, and .txt will be added to the end of the value.

As far as I can see, $CuestaP will expand to nothing, and most of your data will go to a file named .txt (a hidden file in the current directory).

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.