Below I have a script that is working and is used to find file sizes in our Object Storage Architecture running on Ubuntu. The issue I am having is the script is written for checking one account at a time which works fine, but if I wanted to modify it to parse through multiple accounts one after another how would this be possible. Is there a way to pass a list of parameters from another text file to replace the variables at the top of the script?
For example if another text file, test.txt, had the following lines:
auto02 FfiBftkjgS8hnQn79Arj7PiHfvtsgn
qa04 s67aeYD6521pPgt7TknvGxKvF9WxNF
Is it possible to grab the user and key from the file above and replace it with the varibles at the top of this script in some kind of loop to go through all of the accounts?
#!/bin/bash
# Variables to be set
auth=http://sslabapi/auth/v1.0 # Auth URL
user=qa04 # Username
key=s67aeYD6521pPgt7TknvGxKvF9WxNF # Password
size=500000 # Minimum file size in bytes
# Env variables set
ST_AUTH="$auth"
ST_USER="$user"
ST_KEY="$key"
# Env variables exported
export ST_AUTH
export ST_KEY
export ST_USER
# Timestamp function
timestamp() {
date +"%Y-%m-%d %T"
}
# Main Loop
containerList="$(swift list)"
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" >> bigFiles.txt
echo "$(timestamp): Account for $user" >> bigFiles.txt
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" >> bigFiles.txt
echo "Starting check for files > $size bytes in the $user user account..."
for i in $containerList; do
echo "*************************" >> bigFiles.txt
echo "Container $i" >> bigFiles.txt
echo "*************************" >> bigFiles.txt
echo "Container $i"
IFS=$'\n'
olist=($(swift list -l $i))
for a in ${olist[@]}; do
osize=`echo "$a" | awk '{print $1}'`
if [ $osize -gt "$size" ]; then
echo "Found one: $a"
echo "$a" >> bigFiles.txt
fi
done
done
Below is an edited version of my script to have the additions provided:
#!/bin/bash
while read -r user key
do
# Variables to be set
auth=http://sslabapi/auth/v1.0 # Auth URL
#user=$user # Username
#key=$key # Password
size=500000 # Minimum file size in bytes
# Env variables set
ST_AUTH="$auth"
ST_USER="$user"
ST_KEY="$key"
# Env variables exported
export ST_AUTH
export ST_KEY
export ST_USER
# Timestamp function
timestamp() {
date +"%Y-%m-%d %T"
}
# Main Loop
containerList="$(swift list)"
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" >> bigFiles.txt
echo "$(timestamp): Account for $user" >> bigFiles.txt
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" >> bigFiles.txt
echo "Starting check for files > $size bytes in the $user user account..."
for i in $containerList; do
echo "*************************" >> bigFiles.txt
echo "Container $i" >> bigFiles.txt
echo "*************************" >> bigFiles.txt
echo "Container $i"
IFS=$'\n'
olist=($(swift list -l $i))
for a in ${olist[@]}; do
osize=`echo "$a" | awk '{print $1}'`
if [ $osize -gt "$size" ]; then
echo "Found one: $a"
echo "$a" >> bigFiles.txt
fi
done
done
echo "...finished check!"
done < test.txt
user=$user # Usernameandkey=$key # Password. Theread -r user keyalready reads the variablesuserandkey.echo "user=$user"andecho "key=$key"in the loop or after setting the exported variablesecho "ST_USER=$ST_USER"... Which command actually uses the variablesST_USERetc.?