You may easily check your shell scripts using ShellCheck online (also available as a standalone tool).
In this case, it will point out that the if-statement needs spaces, after
[ and before
], and that you need a
; (or a newline) before the
then on the same line.
When you've fixed that, it will go on to tell you that
USER_NAME is used without being initialized to anything. This is because you also have a
user_name variable (case matters). The same is true for
It also tells you to use
read -r to stop
read from mangling
\ (could be important for passwords, for example), and that you should double quote the variables when calling
sqlplus to prevent the shell from accidentally doing file name globbing and word splitting (again this is important if the password, for example, contains file globbing characters like
*, or spaces).
Indenting the code will make it more readable too:
read -r -p 'please enter username: ' user_name
IFS= read -rs -p 'please enter password: ' pass
printf 'ORACLE_SID = %s\n' "$ORACLE_SID"
if [ "$sid" = 'Test' ]; then
echo 'Cannot copy' >&2
sqlplus -s -l "$user_name/$pass@$sid" <<'SQL_END'
copy from scott/tiger@orcl insert EMP using select * from EMP
Here I've also made it possible to use passwords with leading or trailing space characters by temporarily setting
IFS to an empty string for the password reading
The logic was also changed to bail out if
Test. This avoids having the main operational part of the script in an