1

I currently have this function that will determine what account the database user should be:

calc_id_value(){
case $id_opt in
  "") id_value="$script_id@$SID"
      sql_in_db=$id_value
  ;;
  sys) id_value="sys/$sys_password@$SID as sysdba"
       sql_in_db=$id_value
  ;;
  system) id_value="system/$system_password@$SID"
          sql_in_db=$id_value
  ;;
  *) echo "Error in calc_id_value()"
  ;;
esac
}

Later, I will read each line of a list. Each line is a database. The below loop will go to each database and run the function called sql_in which just logs in and runs a file. The problem is that it will not go to the next database. It seems it is because it still interprets $id_value = $script_id@$database1 when it should be = to $script_id@databasen.

while read sid
do
  SID=$sid
  export SID
#  calc_id_value
  sql_in_db=$id_value
  sql_in
done < "$list_value"

The problem is that it will not go to the next database. It seems it is because it continues to interpret $id_value = $script_id@$database1 when it should be = to $script_id@databasen.

However, it works if I uncomment out calc_id_value within the loop. If I allow this, it feels like bad programming practice. I will then be forcing my current iteration of a loop to go back through a case statement that it. Is there a better way? If possible, I would like to be able to dynamically change the $SID part of the id_value variable after each line read.

  • How do you expect the value of id_value to change if you don't call calc_id_value in the loop? Also, what is id_opt: it's defined nowhere, yet it is used in the function. – NickD Jul 19 '17 at 19:22
  • @Nick, thank you for taking interest. id_opt is a parameter read in by getops function. I was trying not to over burden with code, I apologize if it was a misstep – exit_1 Jul 19 '17 at 19:24
  • It hurts my eyes to see names like $id_value. Since every variable holds a value, it's ok to simply call it $id – kubanczyk Jul 19 '17 at 19:27
  • @kubanczyk, that's a fair point that I will take into my revisions, thanks – exit_1 Jul 19 '17 at 19:29
  • @Nick, I was expecting it to change because I was changing the value of $SID which is part of the definition of id_value. However, I'm clearly misunderstanding the concept in that. – exit_1 Jul 19 '17 at 19:34
1

It's doable but it's likely harmful. I would advice against that level of abstraction when scripting.

Consider whoever will read your script in future. In case of other source code, they will be programmers; in case of scripts they will probably be users or admins who will need some small fixes or small customizations. It's uneconomical to use their time and mental capabilities just to save a little bit of processor time (or to get rid of subjective code smells). KISS is the paradigm for scripts.

Therefore please uncomment the code in the loop and don't "feel bad".

  • 1
    I agree, I wouldn't want to waste the time of other admins. I couldn't decide if making them go read another function over again is a waste of their time or valiantly simple, as opposed to just trying to reset the variable in one or 2 lines within the loop. Thank you for your input. – exit_1 Jul 19 '17 at 19:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.