Variable lost outside a loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variable lost outside a loop
# 1  
Old 03-07-2016
Variable lost outside a loop

Hello everybody,

I have a database backup code, using mysqldump, and I would like to ignore some tables, after getting the variable echo outside the loop, I ve got the last, the first one was overridden:

Code:
ignoreTables=('visitors_15012016 visitors_Original')

for line in $ignoreTables
do

echo 'mysqldump --ignore-table='$line
done

echo 'li '$line

mysqldump --log-error=/var/log/mysql-dump/dump.log -u USER -pPASSWD DBName $line | bzip2 > /path2file/file.sql.bz2

I am looking form something like:

Code:
mysqldump --log-error=/var/log/mysql-dump/dump.log -u USER -pPASSWD DBName --ignore-table=visitors_15012016 --ignore-table=visitors_Original | bzip2 > /path2file/file.sql.bz2

Thanks in advance
# 2  
Old 03-08-2016
Code:
ignoreTables=( visitors_15012016 visitors_Original )

ignoreString=''
for tbl in ${ignoreTables[@]}
do
    ignoreString=${ignoreString}"--ignore-table="${tbl}" "
done

mysqldump --log-error=/var/log/mysql-dump/dump.log -u USER -pPASSWD DBName $ignoreString | bzip2 > /path2file/file.sql.bz2

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 03-08-2016
Thank you dude, could you explain the code for me please? I am still a newbie
# 4  
Old 03-08-2016
bash code:
  1. # create array. take care of the syntax .. spaces and parenthesis
  2. ignoreTables=( visitors_15012016 visitors_Original )
  3.  
  4. # take an initial dummy empty variable for building the required string
  5. # we need --ignore-table=tbl1 --ignore-table=tbl2 and so on...
  6. ignoreString=''
  7. # loop over the array. ${arr_var[@]} simply gives back all the elements of the array
  8. for tbl in ${ignoreTables[@]}
  9. do
  10.     # build the string variable. with each pass of the loop, the new table name is appended to the old one along with the mysqldump switch "--ignore-table"
  11.     ignoreString=${ignoreString}"--ignore-table="${tbl}" "
  12. done
  13. # now at the end of loop, ignoreString contains
  14. # "--ignore-table=tbl1 --ignore-table=tbl2"
  15. # use it in the below command
  16.  
  17. mysqldump --log-error=/var/log/mysql-dump/dump.log -u USER -pPASSWD DBName $ignoreString | bzip2 > /path2file/file.sql.bz2

Last edited by balajesuri; 03-08-2016 at 01:12 AM..
# 5  
Old 03-08-2016
Oh yeah got it, I use to do it in PHP but not the same concept:

PHP Code:
$string=''
for(){
$string .='something else';

// end for
echo $string
I think if my array is something like WILL NOT work, will return the last element in the array, I use to set my arrays as below:

Code:
ignoreTbl=( 'visitors_15012016' 'visitors_Original' )

There are 2 elements there:

Code:
ignoreTables=( 'visitors_15012016 visitors_Original' )
entries1=${#ignoreTables[@]}
echo 'ignoreTables has '$entries1' elements' // WILL RETURN 1

ignoreTbl=( 'visitors_15012016' 'visitors_Original' )
entries2=${#ignoreTbl[@]}
echo 'ignoreTbl has '$entries2' elements' // WILL RETURN 2


Last edited by Abu Rayane; 03-09-2016 at 12:32 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

2. Shell Programming and Scripting

Attempting to pass my array as a nested parameter to loop. A little lost.

I want to pass this array as a parameter. IFS=$'\n' fortune_lines=($(fortune | fold -w 30 )) Inside of this line screen -p 0 -S ${SCREEN_SESSION} -X stuff "`printf "say ${fortune_lines}\r"`" And I am lost at this point. I am thinking something like this? Then make it loop.. ... (7 Replies)
Discussion started by: briandanielz
7 Replies

3. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

4. UNIX for Dummies Questions & Answers

Loop and variable not exactly variable: what's wrong

Hello guys, This truly is a newbie question. I'm trying to make a loop to execute simultaneous commands indefinitely while using variable. Here is how my mess looks like (this is just an example): #!/bin/bash IP=`shuf -n 1 IP.txt` # I figured this would be easier to select random lines... (4 Replies)
Discussion started by: bobylapointe
4 Replies

5. Shell Programming and Scripting

printing variable with variable suffix through loop

I have a group of variables myLINEcnt1 - myLINEcnt10. I'm trying to printout the values using a for loop. I am at the head banging stage since i'm sure it has to be a basic syntax issue that i can't figure out. For myIPgrp in 1 2 3 4 5 6 7 8 9 10; do here i want to output the value of... (4 Replies)
Discussion started by: oly_r
4 Replies

6. Shell Programming and Scripting

[SHELL: /bin/sh] For loop using variable variable names

Simple enough problem I think, I just can't seem to get it right. The below doesn't work as intended, it's just a function defined in a much larger script: CheckValues() { for field in \ Group_ID \ Group_Title \ Rule_ID \ Rule_Severity \ ... (2 Replies)
Discussion started by: Vryali
2 Replies

7. Shell Programming and Scripting

bash and ksh: variable lost in loop in bash?

Hi, I use AIX (ksh) and Linux (bash) servers. I'm trying to do scripts to will run in both ksh and bash, and most of the time it works. But this time I don't get it in bash (I'm more familar in ksh). The goal of my script if to read a "config file" (like "ini" file), and make various report.... (2 Replies)
Discussion started by: estienne
2 Replies

8. UNIX for Dummies Questions & Answers

Variable gets lost

Hi Please help! Am using unix AIX... The variable i use in the beggining of the shell script gets lost after i call the sqlldr and sql plus...no changes being done anywhere else... Thanks... (1 Reply)
Discussion started by: esh.mohan
1 Replies

9. Filesystems, Disks and Memory

Lost Data Lost Admin

First time so excuse my ignorance please. I may not be accurately describing the issue. I have inherited a small lab mostly SUN V120s. We lost power and are trying to recover. Nope no backups... The primary issue I have is 1 box is an Oracle Server. It has 2 36Gb harddrives. I am able to... (3 Replies)
Discussion started by: murphsr
3 Replies
Login or Register to Ask a Question