Bash script to run several .sql files


#1

I am trying to create a bash script that will loop through a series of .sql files. I’ve tried creating the array in two different ways, and I get no response. The scripts have auth to run. Any suggestions will be greatly super appreciated. Below you will see both of my attempts at the script.

#!/bin/sh

# Assigns values to the variables username, password, and directory.
username="student"
password="student"
directory="/home/student/Data/cit325/oracle/final"


# Allocates an array of strings to be iterated over.
cmd=("base_t.sql"
  "create_tolkien.sql" 
  "dwarf_t.sql" 
  "elf_t.sql" 
  "goblin_t.sql" 
  "hobbit_t.sql" 
  "maia_t.sql" 
  "man_t.sql" 
  "orc_t.sql" 
  "noldor_t.sql" 
  "silvan_t.sql" 
  "sindar_t.sql" 
  "teleri_t.sql" 
  "type_validation.sql" 
  "insert_instances.sql" 
  "query_instances.sql")

# Iterate over each sql script stored in the array and attempt to pass it
# to sqlplus. Any output will be hidden from the user because we are sending
# the values to /dev/null.
for i in ${cmd[@]}; do
  sqlplus -s ${username}/${password}@ @${directory}/${i} > /dev/null
done

# Output any of the information sqlplus' spool command outputs to
# type_validation.txt and query_instances.txt
cat type_validation.txt
cat query_instances.txt

--------------------------------------------------------------------------------------------------
#!/usr/bin/bash
 
# Assign user and password
username="student"
password="student"
directory="/home/student/Data/cit325/oracle/final"
 
echo "User name:" ${username}
echo "Password: " ${password}
echo "Directory:" ${directory}
 
# Define an array.
declare -a cmd
 
# Assign elements to an array.
cmd[0]="base_t.sql"
cmd[1]="create_tolkien.sql"
cmd[2]="dwarf_t.sql"
cmd[3]="elf_t.sql"
cmd[4]="goblin_t.sql"
cmd[5]="hobbit_t.sql"
cmd[6]="maia_t.sql"
cmd[7]="man_t.sql"
cmd[8]="orc_t.sql"
cmd[9]="noldor_t.sql"
cmd[10]="silvan_t.sql"
cmd[11]="sindar_t.sql"
cmd[12]="teleri_t.sql"
cmd[13]="type_validation.sql"
cmd[14]="insert_instances.sql"
cmd[15]="query_instances.sql"
 
# Call the array elements.
for i in ${cmd[*]}; do
  sqlplus -s ${username}/${password} @${directory}/${i} > /dev/null
done
 
# Print query log files.
cat type_validation.txt
cat query_instances.txt