我在获取变量$myname
、以及对另一个文件 中的所有记录进行计数以附加到文本文件 的$filename
方法时遇到问题。代码在下面,我不明白为什么它们不附加。wc -l < hs_alt_HuRef_chr10.fa >> "$CuestaP.txt
CuestaP.txt
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
我的输出:你好,我的名字是: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
问题是文件名$CuestaP.txt
。要么您想$
真正成为文件名的一部分,然后您需要单引号或反斜杠:'$CuestaP.txt'
, \$CuestaP.txt
。或者这应该是一个变量,但后来你们都忘记定义它并将其与文字混合。
您的数据在文件中.txt
答案2
[ -s *.fa ]
如果有多个文件名以.fa
.您可以使用单个文件名(hs_alt_HuRef_chr10.fa
?)来测试它,或者循环:
for name in *.fa; do
if [ -s "$name" ]; then
printf '"%s" has data in it\n' "$name"
fi
done >>"$CuestaP.txt"
这与其余大部分输出语句一起假设CuestaP
是一个变量。 $CuestaP.txt
将扩展到该变量的内容,并将.txt
添加到值的末尾。
据我所知,$CuestaP
将扩展到任何内容,并且您的大部分数据将进入一个名为.txt
(当前目录中的隐藏文件)的文件。