how to hide warning in bash script [duplicate] - bash

This question already has answers here:
Suppress warning output in bash
(3 answers)
here-document gives 'unexpected end of file' error
(10 answers)
Closed 6 years ago.
I have a bash script I've been using for ages. It has a few lines it just start/stop some jobs in database. It is very simple and it never fails. Last year, it facts in December so a few days ago :) I migrated whole data to a new machine. And then my problem starts.
Now script works as it worked before but now it returns warning. Below output:
[oracle#SVMSLUATTIADB1 ~]$ ./batch.sh stop
./batch.sh: line 87: warning: here-document at line 85 delimited by end-of-file (wanted `!')
./batch.sh: line 87: warning: here-document at line 85 delimited by end-of-file (wanted `!')
Batch_Executer_1 [NOT OK]
Batch_Executer_2 [NOT OK]
[oracle#SVMSLUATTIADB1 ~]$
The piece of code mentioned in warning is:
batch_stop()
{
for BATCH in $BATCHES
do
SQLRES=`sqlplus -S "/ as sysdba" << !
exec dbms_scheduler.disable('"AIT"."${BATCH}"', TRUE);
` << line 87
done
}
So the question is how to avoid/hide this warnings. It is not critical, script works as it worked but the view of warning disturbs. I see that before I had bash version 3.2.25 but now it is bash 4.1.2.
I tried to modified my code and redirected output :
{SQLRES=`sqlplus -S "/ as sysdba" << !
exec dbms_scheduler.disable('"AIT"."${BATCH}"', TRUE);
` } 2>/dev/null
but it didn't help :(

The warning is caused by invalid here document syntax. Namely, the second limit word ("!") is missing.
Also, don't use back quotes for command substitution. Use the $(...) syntax instead, as it is more readable, and it can be nested.
The fixed version:
SQLRES=$(sqlplus -S "/ as sysdba" << !
exec dbms_scheduler.disable('"AIT"."${BATCH}"', TRUE);
!
)
Note, the here document ends on a line containing only the limit word (the exclamation mark, in particular). So it is not allowed to indent the second limit word, for example.

Related

bash read variable based on value of a default variable in config.ini file using source

I would like to read my config file and get the value of the default printer. So in this example I would expect to return the result Zebra_GK420D.
My config.ini file as follows:
enable_printing=yes
default_printer=printer1
printer1=Zebra_GK420D
printer2=DYMO_LabelWriter_4XL
create_proof=yes
I use the following bash script:
1 #!/usr/bin/env bash
2
3 #Define filename
4 fConfig=config.ini
5
6 #If file exists, read in variables.
7 if test -f $fConfig ; then
8 source $fConfig
9 fi
10
11 echo The default_printer is: ${default_printer%?}
12
13 echo The name of the default_printer is: $(${default_printer%?})
When I run the script, it returns:
The default_printer is: printer1
./test.sh: line 13: printer1: command not found
How can I fix my bash script so that it returns the following:
The default_printer is: printer1
The name of the default_printer is: Zebra_GK420D
As a side note, since the config.ini file is hosted on a Windows drive each line is returned with \r at the end, so I'm using the %? to remove the last character of each line. The danger here is that the final line in the file does not have \r at the end. What can I use instead of %? to remove only \r as opposed to blindly removing the last character?
Thank you.
The answer is in the error message: command not found. Look at your line 13:
echo The name of the default_printer is: $(${default_printer%?})
$(command) is used in bash to get the output of a command. So you explicitly asked to execute the printer name as a command, and, predictably, the command does not exist. Try:
echo The name of the default_printer is: ${default_printer%?}

syntax error: unexpected end of file while executing a query inside while block [duplicate]

This question already has answers here:
Bash syntax error: unexpected end of file
(21 answers)
Closed 6 years ago.
I have prepared a shell script which takes 6 char(s) from a file and insert a particular record using sql insert query within a while loop(as the number of records in file is huge).
Upon running the script I am receiving below error -
./TestScriptFor.sh: line 40: syntax error: unexpected end of file
#!/bin/bash
varSIAREQUESTID=$1
varCINAME=$2
#varWORDLENGTH=$3
#DB connection parameter
DBUSER='#USERNAME#'
DBUSERPASSWORD='#PWD#'
DB='oracle'
MYDB='#CONNECTIONSTRING#'
OLDIFS=$IFS
# while loop
while IFS= read -r -n6 LOCATIONID
do
#insert into database
sqlplus -s ${DBUSER}/${DBUSERPASSWORD}#${MYDB} <<EOT
set linesize 32767
set feedback off
set heading off
insert into nbn_sia_locationids (siarequestid,locationid,ciname) values('$varSIAREQUESTID','$LOCATIONID','$CINAME');
exit
EOT
echo "$LOCATIONID"
# display one character at a time
done < TestData.csv
IFS=$OLDIFS
Thanks for your responses.
I did below two steps to resolved the issue -
1. dos2unix TestData.csv - although I had created this file in UNIX itself but did this as well.
2. a new line at the end of the file as suggested by #cdark
regards,
dhananjay

Weird behavior in bashrc: concatenate, same code run/fail [duplicate]

This question already has answers here:
Bash syntax error: unexpected end of file
(21 answers)
Closed 6 years ago.
I have a VM with CentOS 6 and I am loading some scripts from bashrc.
Everything worked fine, but I wanted to copy-paste the same code and scripts in an older backup of same VM, but I got an error: "unexpected end of file". Also the same error had to deal another person when I wanted to share those scripts with him (he had the same VM).
So I started to debug a little and found that one row he didn't liked it was (it was parsing an array:
COUNTER=1
while [[ ! -z ${SCRIPT[$COUNTER]} ]]; do
Also he didn't liked this either (it's not exactly the same with "while" logic, but it does the job):
for i in ${Script[#]}; do
So, I replaced it with:
for ((i = 0; i < ${#SCRIPT[#]}; i++)); do
Now I tryed to get the error name with same piece of code and no more errors occurred.
Also I have this behavior which is the weirdest from all:
Code:
BASH_SCRIPTS_LOCATION='/mnt/hgfs/Shared-workspace/scripts/'
SCRIPT[0]='aliases.sh'
SCRIPT[1]='scripts_config.sh'
SCRIPT[2]='credentials.sh'
SCRIPT[3]='other_functions.sh'
SCRIPT[4]='ssh_functions.sh'
SCRIPT[5]='release_functions.sh'
SCRIPT[6]='test_functions.sh'
for ((i = 0; i < ${#SCRIPT[#]}; i++)); do
loadedScript=${BASH_SCRIPTS_LOCATION}${SCRIPT[$i]}
echo -e "$loadedScript"
done
Terminal output (seems the "concatenate" it is replacing the characters starting from the begging of first String/variable :
aliases.shShared-workspace/scripts/
scripts_config.shworkspace/scripts/
credentials.shed-workspace/scripts/
other_functions.shorkspace/scripts/
ssh_functions.sh-workspace/scripts/
release_functions.shkspace/scripts/
test_functions.shworkspace/scripts/
I think I am using something very inappropriate. But I am not sure what or what I should be looking for.
Any recommandation or advice is welcome.
Thanks!
It doesn't show here but your script has carriage return chars in the shell definition lines. Edit them out (using Notepad++ for instance or tr -d "\015" < yourscript.sh > newscript.sh)
You can redirect your script to a file you'll see all the text in the file.
Carriage return char (asc 13, \r) just resets the cursor without skipping to newline. Every text written after that overwrites the text in the current line. Windows uses that to complement the linefeed character. Windows text mode is like that

Implementing which function in shell [duplicate]

This question already has answers here:
Bash script syntax error "do"?
(3 answers)
Closed 7 years ago.
I'm trying to implement Unix's which function, but keep getting syntax errors, on what I (think) is legal? This is my implementation:
IFS=":"
x=false
for i in $*
do
for j in $PATH
do
if [ -x "${j}/$i" ];then
echo $j/$i
x=true
break
fi
done
if [ $x == false ]; then
echo my_which $i not found in --$PATH--
fi
x=false
done
I keep getting the following error
$ bash which.sh
: command not found:
'which.sh: line 5: syntax error near unexpected token `do
'which.sh: line 5: `do
Your script has DOS newlines. Use dos2unix to convert it, or open it in an editor that can do the conversion for you (in vim, you would run :set fileformat=unix and then save with :w).
$ bash which.sh
: command not found:
'which.sh: line 5: syntax error near unexpected token `do
'which.sh: line 5: `do
See the 's at the beginning of those lines? Those are supposed to be at the end of the line.
What's happening, however, is that your dos have a hidden $'\r' character after them, which sends the cursor back to the beginning of the line. Thus, instead of seeing do as a valid token, or correctly printing
# this is the error you would get if your "do" were really a "do", but it were still
# ...somehow bad syntax.
syntax error near unexpected token `do'
...we get...
# this is the error you get when your "do" is really a $'do\r'
'yntax error near unexpected token `do
...because a carriage return is sitting between the do and the '.

Here document notations [duplicate]

This question already has answers here:
here-document gives 'unexpected end of file' error
(10 answers)
Closed 2 years ago.
I cannot find much explanation regarding a notation in here documents. In my shell script, I am plotting a file with gnuplot.
This code works:
gnuplot <<- EOF
set xlabel "square dimension (inches)"
set ylabel "mean survival time (seconds)"
set term png
set output "${plot_file}.png"
plot "beetle.dat" using 1:2
EOF
However, if I do not include the dash in <<- and just use <<, this code does not work and I get the following error:
./myscript: line 118: warning: here-document at line 104 delimited by end-of-file (wanted `EOF')
./myscript: line 119: syntax error: unexpected end of file
This question might have been asked before, however due to special characters not being recognized, I cannot search for it.
When you say "this code", is it that code - or is it indented ?
Heredocs by default look for a line containing ONLY the delimiter, so no leading tabs or spaces. What the "-" does is remove leading tabs, so you can indent the heredoc (both content and delimiter) prettily in line with the rest of your code.
So if your delimiter is actually indented in the code, it will only be found with the "-"
See 3.6.6 in https://www.gnu.org/software/bash/manual/html_node/Redirections.html

Resources