Why doesn't sh with errexit work with a command list? - error-handling

If I run the following shell script as a normal user, it aborts at line three as expected:
set -o errexit
echo foo > /bar
echo $?
Here is the output:
$ sh test1.sh
test.sh: 3: test.sh: cannot create /bar: Permission denied
However, if the echo command is a part of a compound list, the execution continues after the failing command and prints the exit code:
set -o errexit
{ echo foo; } > /bar
echo $?
Here is the output:
$ sh test2.sh
test.sh: 3: test.sh: cannot create /bar: Permission denied
2
How come the script doesn't abort? On the other hand, if I change the curly braces to parentheses it works like how I would expect.

The POSIX specification states that a shell "may exit" if a redirection error occurs with a compound command.
bash chooses to exit if the compound command is a subshell command ((...)), but otherwise chooses not to. I am not aware of the rationale for this distinction; it may be historical in nature.
set -e has many quirks, and often will not behave the way you expect. Many people advise that you simply not use it.

Related

Executing BTEQ file via shell script (BTEQ: Command not found error)

I'm trying to set up an environment to execute BTEQ script via shell script in the local machine. On running the shell script I'm getting an error of BTEQ: Command not found. Not sure what I'm doing wrong.
I've created a separate .tdlogon file which contains .LOGON credentials. BTEQ script is a simple create table statement that I'm trying to execute.
My .tdlogon file is something like
.logon servername/uname,pwd
I'm calling the file like this
#!/bin/bash
server_path=/Users/xyz/xyz
log_path=/Users/xyz/xyz/logs
echo -e 'Starting the script'>> ${log_path}/test_log.log
cat ${server_path}/.tdlogon ${server_path}/code/temp_query.btq | bteq >> ${log_path}/test_log.log 2>&1
if [ ${rtn_code} -ne 0 ] ; then
echo -e 'Script completed successfully'>> ${log_path}/test_log.log
exit 0
else
echo -e 'Error in the script'>> ${log_path}/test_log.log
exit 1
fi
On executing the above code I'm getting below error in the log file
line 10: bteq: command not found
Appreciate any guidance related to this.
Seems like your Linux is not pointing to the bteq path. Update the bteq path:
export PATH=/usr/bin/bteq:$PATH
And, in some cases, there will be bteq32 instead of bteq in that case set path as:
export PATH=/usr/bin/bteq32:$PATH

Run RapSearch-Program with Torque PBS and qsub

My problem is that I have a cluster-server with Torque PBS and want to use it to run a sequence-comparison with the program rapsearch.
The normal RapSearch command is:
./rapsearch -q protein.fasta -d database -o output -e 0.001 -v 10 -x t -z 32
Now I want to run it with 2 nodes on the cluster-server.
I've tried with: echo "./rapsearch -q protein.fasta -d database -o output -e 0.001 -v 10 -x t -z 32" | qsub -l nodes=2 but nothing happened.
Do you have any suggestions? Where I'm wrong? Help please.
Standard output (and error output) files are placed in your home directory by default; take a look. You are looking for a file named STDIN.e[numbers], it will contain the error message.
However, I see that you're using ./rapsearch but are not really being explicit about what directory you're in. Your problem is therefore probably a matter of changing directory into the directory that you submitted from. When your terminal is in the directory of the rapsearch executable, try echo "cd \$PBS_O_WORKDIR && ./rapsearch [arguments]" | qsub [arguments] to submit your job to the cluster.
Other tips:
You could add rapsearch to your path if you use it often. Then you can use it like a regular command anywhere. It's a matter of adding the line export PATH=/full/path/to/rapsearch/bin:$PATH to your .bashrc file.
Create a submission script for use with qsub. Here is a good example.

Unable to run a postgresql script from bash

I am learning the shell language. I have creating a shell script whose function is to login into the DB and run a .sql file. Following are the contents of the script -
#!/bin/bash
set -x
echo "Login to postgres user for autoqa_rpt_production"
$DB_PATH -U $POSTGRESS_USER $Auto_rpt_production$TARGET_DB -p $TARGET_PORT
echo "Running SQL Dump - auto_qa_db_sync"
\\i auto_qa_db_sync.sql
After running the above script, I get the following error
./autoqa_script.sh: 39: ./autoqa_script.sh: /i: not found
Following one article, I tried reversing the slash but it didn't worked.
I don't understand why this is happening. Because when I try manually running the sql file, it works properly. Can anyone help?
#!/bin/bash
set -x
echo "Login to postgres user for autoqa_rpt_production and run script"
$DB_PATH -U $POSTGRESS_USER $Auto_rpt_production$TARGET_DB -p $TARGET_PORT -f auto_qa_db_sync.sql
The lines you put in a shell script are (moreless, let's say so for now) equivalent to what you would put right to the Bash prompt (the one ending with '$' or '#' if you're a root). When you execute a script (a list of commands), one command will be run after the previous terminates.
What you wanted to do is to run the client and issue a "\i ./autoqa_script.sh" comand in it.
What you did was to run the client, and after the client terminated, issue that command in Bash.
You should read about Bash pipelines - these are the way to run programs and input text inside them. Following your original idea to solving the problem, you'd write something like:
echo '\i auto_qa_db_sync.sql' | $DB_PATH -U $POSTGRESS_USER $Auto_rpt_production$TARGET_DB -p $TARGET_PORT
Hope that helps to understand.

change user to oracle and run sql in a shell script

I am a root user and in a shell script I would like to change user to oracle than run a sql script, I tried following;
#!/bin/sh
portStatus=`lsof -ni:5060`
if [ ${#portStatus} -ne 0 ]
then
sudo -u oracle << EOF
/oracle/product/102/db/bin/sqlplus -s a513s6p4/a513s6p4 #/oracle/product/102/db/GW_EP_List.sql;
EOF
else
exit
fi
it gives me following error;
./deneme2.sh: syntax error at line 12: `end of file' unexpected
Can you please let me know what might be the problem?
Thanks,
Halit
When using here documents the closing string MUST be at the beginning of the line!
Try
#!/bin/sh
portStatus=`lsof -ni:5060`
if [ ${#portStatus} -ne 0 ]
then
sudo -u oracle << EOF
/oracle/product/102/db/bin/sqlplus -s a513s6p4/a513s6p4 #/oracle/product/102/db/GW_EP_List.sql;
EOF
else
exit
fi
You can use su. Remember get environment with su -:
COMMAND="/oracle/product/102/db/bin/sqlplus -s a51... "
su - oracle -c $COMMAND
A nice sample oracle-base site, Automating Database Startup and Shutdown on Linux Post:
case "$1" in
'start')
# Start the Oracle databases:
# The following command assumes that the oracle login
# will not prompt the user for any values
su - $ORA_OWNER -c "$ORA_HOME/bin/lsnrctl start"
su - $ORA_OWNER -c $ORA_HOME/bin/dbstart
touch /var/lock/subsys/dbora
;;
sudo -u oracle /oracle/product/102/db/bin/sqlplus -s a513s..........
You don't need EOF here. Execute your sqlplus command like above. In this case your oracle user must be a sudo user.
If oracle is a normal user
su - oracle -c "/oracle/product/102/db/bin/sqlplus -s a513s.........."
A little more about su command (From man page):
The su command is used to become another user during a login session.
Invoked without a username, su defaults to becoming the superuser.
The optional argument - may be used to provide an environment similar
to what the user would expect had the user logged in directly.
Additional arguments may be provided after the username, in which case
they are supplied to the user's login shell. In particular, an
argument of -c will cause the next argument to be treated as a command
by most command interpreters. The command will be executed by the
shell specified in /etc/passwd for the target user.

sqlcmd runs script but script does not affect database

So we have a .bat file that runs SQL scripts, e.g.
#ECHO --- 03_Case6395_Publication.sql --- >> dbupt.log
sqlcmd -U %1 -P %2 -S %3 -d %4 -i 03_Case6395_Publication.sql -k -b >> dbupt.log
IF ERRORLEVEL 1 GOTO ErrorTag
The script runs and gives no errors, but the script doesn't actually affect the database. In the example above, here is what is being run:
IF NOT EXISTS (SELECT 1 FROM [dbo].[syscolumns] WHERE [NAME] = N'MandatoryInList' AND [ID] = object_id(N'Pub_Type'))
BEGIN
ALTER TABLE [dbo].[Pub_Type] ADD [MandatoryInList] bit NULL CONSTRAINT [DF_PubType_MandatoryInList] DEFAULT (0)
END
ELSE
BEGIN
ALTER TABLE [dbo].[Pub_Type] ALTER COLUMN [MandatoryInList] bit NULL
END
GO
The script is pretty simple and you would expect it to add a column called MandatoryInList, however it doesn't. The odd thing is there are no syntax errors in the script and when I run it from SQL Server Management Studio, it runs fine. There's no issues with permissions or which user is running the script because there are other scripts that run fine in the batch file.
I should also mention that this runs from a GUI by creating a new process and running it but not showing the command window. But regardless, I've been running the tool like this for months without any issues.
Any ideas?
So it works from Management Studio but not from your batch file that calls sqlcmd. Strange...
I don't know what could cause this, but here are some ideas:
If you run the same "sqlcmd" command manually from the command prompt, rather than from your batch file, does it work?
If you run the batch file from the command prompt, rather than invoking it from the GUI app, does it work?
What's it writing to your dbupt.log file? Does it have the header "--- 03_Case6395_Publication.sql --- ", and it's empty after that?
Can you add some PRINT statements to the script, so you can see whether it's executing the IF or the ELSE part?
Can you try redirecting the error stream to your log file as well, using the "2>&1" operator? E.g.:
sqlcmd -U %1 -P %2 -S %3 -d %4 -i 03_Case6395_Publication.sql -k -b >> dbupt.log 2>&1
Can you try changing "IF ERRORLEVEL 1" to "IF NOT ERRORLEVEL 0"?
Have you tried using SQL Profiler to see what SQL is actually executing? Does anything show up?