Loop help

Hi friends,

I have a program below where I need to input keyspace name so that program executing correctly, but I want to pass multiple values in keyspace in same time so that in single go, it should complete all keyspaces names that I pass to program.

#!/bin/bash

keyspace=$1
bkp_name="bkp-$$"
data_dir="/var/lib/data/"

if [ -z "${keyspace}" ]; then
    echo "Usage export.sh [keyspace]"
    exit 1
fi

echo "Create snapshot named: ${bkp_name}"
nodetool snapshot "${keyspace}" -t "${bkp_name}"

echo "Preparing backup file"
for name in $(find  "${data_dir}/${keyspace}/"*"/snapshots/${bkp_name}" -type f); do
        new=$(echo "$name" | sed -e "s#${data_dir}/##g" -e "s#\([^/]\+\)/\([^-]\+\).\+/snapshots/${bkp_name}/\([^/]\+\)\$#\1/\2/\3#g")
        mkdir -p "${bkp_name}/$(dirname $new)"
        cp "$name" "${bkp_name}/$new"
done

echo "Remove snapshot named: ${bkp_name}"
nodetool clearsnapshot -t "${bkp_name}" "${keyspace}"

echo "Dump keyspace and table creation instruction"
cqlsh -e "desc \"${keyspace}\";" > "${bkp_name}/${keyspace}.sql"

echo "Create tar file: ${keyspace}.tar.gz"
cd "${bkp_name}"
tar -czf "../${keyspace}.tar.gz" .
cd -

echo "Remove temporary files"
rm -rf "${bkp_name}"

------ Post updated at 08:41 AM ------

tried to use first two line
for arg; do
keyspace=$1

but its keep looping...

This

#!/bin/bash
echo "arg count = $#"

while [ $# -gt 0 ]
do
    echo $1
    shift
done

reads through the parameters passed to the script, making each parameter be the first one using the shift keyword

So if you wrap your script -- yourscript.sh in my example - with the little script above:

#!/bin/bash
echo "arg count = $#"

while [ $# -gt 0 ]
do
    echo $1
    shift
    /path/to/yourscript.sh "$1"
done

you use your new script (example: new.sh)

#!/bin/bash

/path/to/new.sh  keyspace1 keyspace2  # go on to [ ....  keyspace9999 ]
exit

When you create the new script be sure to make it executable:

chmod +x new.sh

I went into a lot of extra detail because I think that you do not know shell scripting very well and other newbies may have similar questions.

1 Like

Hi Thank you for reply.

Thank you for detailed explanation.

I tried with your solution wrapping my code in your code...

However its taking only one arugement

#!/bin/bash
echo "arg count = $#"

while [ $# -gt 0 ]
do
    echo $1
    shift
    /abexportcp.sh "$1"
done

and running this progream as

$> sh abexportcp.sh system_traces system -- here these are two keyspace names

when i see log on screen..it s saying argument is 2 but taking only one for doing backu
$ >sh new.sh system_traces system
arg count = 2
system_traces
Create snapshot named: bkp-130259
Requested creating snapshot(s) for [system] with snapshot name [bkp-130259] and options {skipFlush=false}
Snapshot directory: bkp-130259
Preparing backup file
Remove snapshot named: bkp-130259
Requested clearing snapshot(s) for [system] with snapshot name [bkp-130259]

Ideally, it has to work upon system_traces is first keyspace name , second keyspace name is system...

any suggestions

Please use code tags.

You got one bit backwards, so your program ends up throwing away the first argument before you use it. Try reversing these two lines.

#!/bin/bash
echo "arg count = $#"

while [ $# -gt 0 ]
do
    echo $1
    /abexportcp.sh "$1"
    shift # Eliminates an argument, so put it after abexport
done
1 Like

A for loop cycles over the script arguments by default:

bkp_name="bkp-$$"
data_dir="/var/lib/data/"
for keyspace
do
  echo do something with "$keyspace"
done

Replace the echo command with the remainder of your script!

1 Like

@Corona688,

Wow that works, just moving the shift does work..Thank you

@ MadeInGermany

I tried your suggestion, but takes only first arugement twice..I liked this method as i dont need to put another script..however its not working ..here the tried code

#!/bin/bash

keyspace=$1
bkp_name="bkp-$$"
data_dir="/var/lib/data/"
for keyspace
do
if [ -z "${keyspace}" ]; then
    echo "Usage export.sh [keyspace]"
    exit 1
fi

echo "Create snapshot named: ${bkp_name}"
nodetool snapshot "${keyspace}" -t "${bkp_name}"

echo "Preparing backup file"
for name in $(find  "${data_dir}/${keyspace}/"*"/snapshots/${bkp_name}" -type f); do
        new=$(echo "$name" | sed -e "s#${data_dir}/##g" -e "s#\([^/]\+\)/\([^-]\+\).\+/snapshots/${bkp_name}/\([^/]\+\)\$#\1/\2/\3#g")
        mkdir -p "${bkp_name}/$(dirname $new)"
        cp "$name" "${bkp_name}/$new"
done

echo "Remove snapshot named: ${bkp_name}"
nodetool clearsnapshot -t "${bkp_name}" "${keyspace}"

echo "Dump keyspace and table creation instruction"
cqlsh 172.20.24.21 -e "desc \"${keyspace}\";" > "${bkp_name}/${keyspace}.sql"

echo "Create tar file: ${keyspace}.tar.gz"
cd "${bkp_name}"
tar -czf "../${keyspace}.tar.gz" .
cd -

echo "Remove temporary files"
rm -rf "${bkp_name}"
done

When I run

[root]# sh abexportcptest.sh system_traces system
Create snapshot named: bkp-8119
Requested creating snapshot(s) for [system_traces] with snapshot name [bkp-8119]                                                                                                              and options {skipFlush=false}
Snapshot directory: bkp-8119
Preparing backup file
Remove snapshot named: bkp-8119
Requested clearing snapshot(s) for [system_traces] with snapshot name [bkp-8119]
Dump keyspace and table creation instruction
Create tar file: system_traces.tar.gz
/home
Remove temporary files
Create snapshot named: bkp-8119
Requested creating snapshot(s) for [system_traces] with snapshot name [bkp-8119]                                                                                                              and options {skipFlush=false}
Snapshot directory: bkp-8119
Preparing backup file
Remove snapshot named: bkp-8119
Requested clearing snapshot(s) for [system_traces] with snapshot name [bkp-8119]
Dump keyspace and table creation instruction
Create tar file: system_traces.tar.gz
/home
Remove temporary files

Its creating system-trace twice.

Apologies for my poor knowledge if at all i did any mistake here..

------ Post updated at 01:08 AM ------

@ MadeInGermany

My apologies, there is typo in program...it did not pickup the loop Now your method too work :slight_smile: thank you

------ Post updated at 01:09 AM ------

@ MadeInGermany

it works now..I mistyped keyspace as keysacpe ... so it did not pickup loop

Thank you very much

The following won't work as expected:

if [ -z "${keyspace}" ]; then
    echo "Usage export.sh [keyspace]"
    exit 1
fi

Move this before the for loop and test $1 (or [ $# -eq 0 ])

if [ -z "$1" ]; then
    echo "Usage export.sh [keyspace]"
    exit 1
fi
for keyspace
do
...

Thank you
@MadeInGermany

It working both ways..putting for loop before or after..Its doing correctly as expected

Only mistake i did earlier, I mis-spelled For keyspace as for keysapce :slight_smile:

thank you very much for your time

Well, if you run your script without a (keyspace-)argument, you'll see the difference.