-bash: eval: line 109: unexpected EOF while looking for matching `"' - bash

if [[ -s $GCMS_ENV && -r $GCMS_ENV ]]
then
echo "">/dev/null ##file exists
if [[ -s $GCMS_PRIV_ENV && -r $GCMS_PRIV_ENV ]]
then
egrep "[A-Z]?=.[a-zA-Z0-9]?" $GCMS_PRIV_ENV | grep -v ^# 2>/dev/null 1> $TMP_FILE
egrep "[A-Z]?=.[a-zA-Z0-9]?" $GCMS_ENV | grep -v ^# 2>/dev/null 1>> $TMP_FILE
else
egrep "[A-Z]?=.[a-zA-Z0-9]?" $GCMS_ENV | grep -v ^# 2>/dev/null 1> $TMP_FILE
fi
{
while read RECORD
do
VAR=$(echo $RECORD|cut -s -d$DELIM -f1)
VAL=$(echo $RECORD|cut -s -d$DELIM -f2-9)
eval export $VAR=$VAL
done
}<$TMP_FILE
else
echo "\n$THIS_FILE\n error:"
echo "The file $GCMS_ENV does not exist, no environment settings!"
return 1
fi
i am trying to run folllowing kSH shell in my linux box while running the same i am facing the following error.
-bash: eval: line 109: unexpected EOF while looking for matching `"'
-bash: eval: line 110: syntax error: unexpected end of file
please let me know anybody have a answer for the same

eval should be avoided whenever possible, especially when it is probably responsible for the syntax error you observe. Use this in place of your while loop.
while read -d"$DELIM" VAR VAL; do
declare -x "$VAR=$VAL"
done < "$TMP_FILE"
You may still have an error that eval triggered, so double check your input files to make sure the assignments have matched ".

Related

Missing "))" in shell script

I'm really sorry I'm a total noob in shell scripting, I looked on the Internet and I didn't find the answer
=> /home/bee/Scripts/chkbsh: 11: /home/bee/Scripts/chkbsh: Syntax error: Missing '))'
#!/bin/sh
for file in $((gawk '/^#!.*( |[/])sh/{printf "%s\0", FILENAME} {nextfile}' /usr/bin/* 2>/dev/null) | xargs -0); do
checkbashisms "$file" >/dev/null 2>&1
if [ "$?" -gt 0 ]
then
sed -i 's:^#!.*/bin/sh:#!/bin/bash:' "$file";
echo "$file" has been processed!
fi
done
echo ":3"
If I change #!/bin/sh in #!/bin/bash everything is okay
You have opening double parentheses, but two single closing parentheses. Place a space after the first (, like this:
for file in $( (gawk '/^#!.*( |[/])sh/{printf "%s\0", FILENAME}
^ there
BTW, it's always good advice to test your scripts with ShellCheck, this way you could have easily spotted above error.
Try
for file in `gawk '/^#!.*( |[/])sh/{printf "%s\0", FILENAME} {nextfile}' /usr/bin/* 2>/dev/null | xargs -0`; do
checkbashisms "$file" >/dev/null 2>&1

Shell script for loop error: Syntax error: word unexpected (expecting "do")

So I realize others have asked similar questions in the past, but when I tried their solutions (using dos2unix, and checking vim for extraneous symbols), they didn't work at all.
#!/bin/bash
#-----------------------------------------------------------------------------
mainDir=/mnt/data1/sam
#-----------------------------------------------------------------------------
----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
cd $fastqDir
for i in `ls *R1_001.fastq.gz`
do
#----------------------------------
filename="${i%_R1_001.fastq.gz}"
#----------------------------------
#adapter-trim
#----------------------------------
echo "Alignment started for $filename" >> ${qcDir}/${filename}_report.txt
filename="${i%_R1_001.fastq.gz}"
echo $filename
echo "Alignment started for $filename" >> ${qcDir}/${filename}_report.txt
sed '/chrM/d;/chrY/d;/random/d;/chrUn/d' < Aligned.out.sam | samtools view -bS -F 4 -q 30 -u - | samtools sort - > ${bamDir}/${filename}.bam
rm Aligned.out.sam
mv Log.final.out ${qcDir}/${filename}_STARLogFinal.txt
echo "Picard started for $filename" >> ${qcDir}/${filename}_report.txt
mv ${bamDir}/${filename}_dedup.bam ${bamDir}/${filename}.bam
samtools index ${bamDir}/${filename}.bam
echo "Picard insert size histogram for $filename" >> ${qcDir}/${filename}_report.txt
INSERT_TXT_FILE="${qcDir}/${filename}.insertSizes.txt"
INSERT_HISTO_FILE="${qcDir}/${filename}.insertSizes.pdf"
echo "Making bigwig for $filename"
lines=$(samtools view -c ${bamDir}/${filename}.bam);\
bedtools genomecov -ibam ${bamDir}/${filename}.bam -bg -scale $(echo "1000000 / ${lines} " | bc -l) -g ${RefDir}/${genome}.chrom.sizes | \
wigToBigWig -clip stdin ${RefDir}/${genome}.chrom.sizes ${bwDir}/${filename}.bw 2> $tmpDir/${filename}_log
echo "macs2 for $filename"
done
I keep getting error
Syntax error: word unexpected (expecting "do")
Sorry if this question is kind of silly, I'm completely new to shell scripting.
It turned out to be extraneous characters after all. I ran the following command and the script started working afterwards. Sorry for the confusion, and thanks for the help everyone!
tr -cd '\11\12\15\40-\176' < file-with-binary-chars > clean-file

Syntax error on simple while loop

This general question has been asked many times and almost always there is an obvious syntax problem. But this seems correct:
cat mixed_encoded.txt |
while read i do
type=${"$(echo "$i" | file -bi -)"#*=}
if [[ $type == 'iso-8859-1' ]]; then
echo "$i" | iconv -f ISO-8859-1 -t UTF-8
else
echo "$i"
fi
done > utf8_encoded.txt
gives
bash: syntax error near unexpected token `done'
Whether pasted as multiline or in one-line mode. With or without the final > utf8_encoded.txt. With the inner quotes escaped or not.
What could be wrong?
cat mixed_encoded.txt |
while read i; do
type=${"$(echo "$i" | file -bi -)"#*=}
if [[ $type == 'iso-8859-1' ]]; then
echo "$i" | iconv -f ISO-8859-1 -t UTF-8
else
echo "$i"
fi
done > utf8_encoded.txt
You are missing a semicolon.
This only fixes the unexpected done token. The substitution is still bad.
This edit fixes the bad substitution:
cat mixed_encoded.txt |
while read i; do
type=$(echo "$i" | file -b --mime-encoding -)
if [[ $type == 'iso-8859-1' ]]; then
echo "$i" | iconv -f ISO-8859-1 -t UTF-8
else
echo "$i"
fi
done > utf8_encoded.txt

conditional binary operator expected in shell script

I was trying a simple program to compare the string values stored on a log file and was getting an error as below,
#!/bin/bash
check_val1="successful"
check_val2="completed"
log="/compile.log"
if [[ grep $check_val1 $log -ne $check_val1 || grep $check_val2 $log -ne $check_val2 ]];
then
echo "No Error"
else
echo "Error"
fi
Error:
./simple.sh: line 7: conditional binary operator expected
./simple.sh: line 7: syntax error near `$check_val1'
./simple.sh: line 7: `if [[ grep $check_val1 $log -ne $check_val1 || grep $check_val2 $log -ne $check_val2 ]];'
Problem is in your if [[...]] expression where you are using 2 grep commands without using command substitution i.e. $(grep 'pattern' file).
However instead of:
if [[ grep $check_val1 $log -ne $check_val1 || grep $check_val2 $log -ne $check_val2 ]]; then
You can use grep -q:
if grep -q -e "$check_val1" -e "$check_val2" "$log"; then
As per man grep:
-q, --quiet, --silent
Quiet mode: suppress normal output. grep will only search a file until a match
has been found, making searches potentially less expensive.
[[ trigers the test command. Test doesn't support testing the exit status of a command just by typing the command

In a unix box, I am taking a list of files as input. If it is found, return the path otherwise return a message "filename file not found"

I have used the find command for this, but it doesnt return any message when a file is not found.
And I want the search to be recursive and return a message "not found" when a file is not found.
Here's the code I have done so far. Here "input.txt" contains the list of files to be searched.
set `cat input.txt`
echo $#
for i in $#
do
find $HOME -name $i
done
Try this:
listfile=input.txt
exec 3>&1
find | \
grep -f <( sed 's|.*|/&$|' "$listfile" ) | \
tee /dev/fd/3 | \
sed 's|.*/\([^/]*\)$|\1|' | \
grep -v -f - "$listfile" | \
sed 's/$/ Not found/'
exec 3>&-
open file descriptor 3
find the files
see if they're on the list (use sed to
send a copy of the found ones to file descriptor 3
strip off the directory name
get a list of the ones that don't appear
add the "Not found" message
close file descriptor 3
Output looks like:
/path/to/file1
/path/somewhere/file2
foo Not found
bar Not found
No loops necessary.
Whats wrong with using a script. I hope this will do.
#!/bin/bash -f
for i in $#
do
var=`find $HOME -name $i`
if [ -z "$var"]
then
var="File not found"
fi
echo $var
done
You can use the shell builtin 'test' to test the existence of a file. There is also an alternative syntax using square brackets:
if [ -f $a ]; then # Don't forget the semicolon.
echo $a
else
echo 'Not Found'
fi
Here is one way - create a list of all the files to grep against. If your implementation supports
grep -q otherwise use grep [pattern] 2&>1 >/dev/null....
find $HOME -type f |
while read fname
do
echo "$(basename $fname) $fname"
done > /tmp/chk.lis
while read fname
do
grep -q "^$fname" /tmp/chk.lis
[ $? -eq 0 ] && echo "$fname found" || echo "$fname not found"
done < /tmp/chk.lis
All of this is needed because POSIX find does not return an error when a file is not found
perl -nlE'say-f$_?$_:"not found: $_"' file

Resources