Lampard
Lampard

Reputation: 404

what @ (at symbol) in square braces on sqlplus shell script mean?

I am working on a shell script to work with sqlplus (oracle), in code part below:

#!/usr/bin/bash

################ Checking tables ###################
TABLES=$(sqlplus -s  ${DBUSER}/${DBPASS}@${DBHOST} <<EOF
...
EOF
)

SuccessTabs=$TABLES

########## Export data from tables to file #########
for TABLE in $TABLES
do
echo "--- Processing $TABLE ---" >> $Log
FILE=$TABLE.csv
TotFiles=$TotFiles$FILE" " -------------------------> (1) Not understand this line ?
sqlplus -s  ${DBUSER}/${DBPASS}@${DBHOST} <<EOF
...
SPOOL $FILE
Select ... FROM $TABLE;
SPOOL OFF
EXIT
EOF

return=$?
if [ $return != 0 ]
then
SuccessTabs=({$SuccessTabs[@]/$TABLE}) -------------> (2) Not understand this line ?
else
echo "--- $TABLE.csv process success ---" >> $Log
fi
done
echo "--- Process all tables success --- " >> $Log
echo "$SuccessTabs " >> $Log
FinalFile=FINAL_"_"${rundate}_${logtime}".csv"
echo "--- Merge all files into $FinalFile ---" >> $Log
cat $TotFiles > $FinalFile

The question is I have two command lines even I know literally what it is used for print out result, but still not understand what its syntax and mechanism.

(1) TotFiles=$TotFiles$FILE" " what is double quote with space at the end used for and why syntax like this ?

(2) SuccessTabs=({$SuccessTabs[@]/$TABLE}) what is [], @, / combine together used for in this command and why syntax like this, especially for [@] , I would like to know what is this ?

Could someone help me to figure out ? Thank you.

Upvotes: 2

Views: 3100

Answers (2)

sjsam
sjsam

Reputation: 21965

TotFiles=$TotFiles$FILE" "

appends to TotFiles, the contents of FILE and a space( that is why you have " ") to existing TotFiles. I would suggest writing this as

TotFiles="${TotFiles}${file} "  # It's not suggested to use UpperCase letters for user variables

Regarding

SuccessTabs=({$SuccessTabs[@]/$TABLE})

It doesn't make much sense to me unless you've declared SuccessTabs as an array. Note that if you have

declare -a var=( array...stuff )
var=( ${var[@]/STUFF_TO_OMIT} ) #omits the `STUFF_TO_OMIT` from the array, ie it gives you everything except `STUFF_TO_OMIT`

If that is what you intended to do then you need to change

SuccessTabs=({$SuccessTabs[@]/$TABLE})

to

SuccessTabs=( ${SuccessTabs[@]/$TABLE} ) # See how curly brackets are placed, again not a good idea using uppercase variables !

Upvotes: 0

zedfoxus
zedfoxus

Reputation: 37109

Let's talk about [@] first. In bash you can declare an array like so:

declare -a testarray=('box' 'cat' 'dog')

You can echo the contents of testarray by doing this:

echo ${testarray[@]}

Result: box cat dog

Now, let's see an interesting behavior:

echo ${testarray[@]/dog}
box cat

See what happened here? Find dog and replace it with nothing. Check this out:

echo ${testarray[@]/dog/pig}
box cat pig

That just replaced dog with pig.

So, the answer to your 2nd question is that if SuccessTabs array contains the same text as in $TABLE, that text is replaced with nothing. The result is assigned back to variable SuccessTabs.


So what's the TotFiles=$TotFiles$FILE" " doing? Taking the same test array example, let's loop through all its values.

$ declare -a testarray=('box' 'cat' 'dog')
$ for item in ${testarray[@]}; do
>     testvar=$testvar$item".."
>     echo $testvar
> done
box..
box..cat..
box..cat..dog..

So, in the 1st loop, $testvar is nothing. $item is box. $testvar$item".." is box... This value is assigned to testvar variable.

In the 2nd loop, $testvar is box... $item is cat. $testvar$item".." is box..cat... This value is assigned to testvar variable.

In the 3rd loop, $testvar is box..cat... $item is dog. $testvar$item".." is box..cat..dog... This value is assigned to testvar variable.

Similarly, if TABLES is a list/array containing box cat dog, $FILE will be box.csv, cat.csv and dog.csv during each iteration of TABLES. TotFiles=$TotFiles$FILE" " will become box.csv cat.csv dog.csv.

See more examples in two places:

My examples above may have inaccuracies. The goal was to give you an example of how to get the information you were looking for.

Upvotes: 7

Related Questions