lazarus78
Posts: 236
Joined: Thu Jul 25, 2013 5:16 pm

Bash scripting help

Mon Jun 16, 2014 10:14 pm

Ive made a simple script to back up specific files for my projects, but in one of my iterations of the script where I use an array, I get an annoying error message.

The error I get is as follows:

Code: Select all

cp: omitting directory `/var/www/'
Please note that my script may not be the best way to do this. Please withhold comments regarding "optimizations". My mind is not that of a programmer, so the best way for me to understand things may not be the best way to do them.

My script is as follows:

Code: Select all

#!/bin/bash

idest=/dellnas02/USERS/Kevin/test
iarray=($(ls /var/www/))
icount=${#iarray[*]}
iloop=0

while [ $iloop -le $icount ]; do
        sudo cp "/var/www/${iarray[$iloop]}" $idest
        #echo "${iarray[$iloop]}"
        iloop=$(($iloop+1))
done
echo "Backup complete."
Now, I know why I am getting the error. If I have my script print out the array, there is a blank line at the end, despite my variable "icount" returning 8 for the number of files I currently have to copy, so the script is looking to copy "/var/www", which is a directory, once it has completed through the list of files, and this is where the error message is coming from. However, I am not trying to copy the directory, just the files listed by the array.

My question is, how can I either suppress this message, or have my script skip the blank line at the end of the array? Aside from this error, it works just fine without any further error.

Thanks for your time.

User avatar
joan
Posts: 14074
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Bash scripting help

Mon Jun 16, 2014 10:25 pm

Perhaps

while [ $iloop -le $icount ]; do

should be

while [ $iloop -lt $icount ]; do

lazarus78
Posts: 236
Joined: Thu Jul 25, 2013 5:16 pm

Re: Bash scripting help

Mon Jun 16, 2014 10:28 pm

holy derp... how did I miss that.

That solves everything. Thank you very much.

User avatar
redhawk
Posts: 3465
Joined: Sun Mar 04, 2012 2:13 pm
Location: ::1

Re: Bash scripting help

Mon Jun 16, 2014 10:28 pm

Adding >/dev/null 2>&1 after $idest should suppress your error messages.
Just out of curiosity why are you copying files one by one would it hurt to use wild cards i.e. cp /var/www/* $idest or use cp -a -r for recursive file / folder copying with preserved attributes??

Richard S.

lazarus78
Posts: 236
Joined: Thu Jul 25, 2013 5:16 pm

Re: Bash scripting help

Mon Jun 16, 2014 10:33 pm

redhawk wrote:Adding >/dev/null 2>&1 after $idest should suppress your error messages.
Just out of curiosity why are you copying files one by one would it hurt to use wild cards i.e. cp /var/www/* $idest or use cp -a -r for recursive file / folder copying with preserved attributes??

Richard S.
Because my mind is simple. My logic was to get a list of the files I want to backup and then copy them over, so this is what I came up with. May not be the best, but it works, and my mind can understand it. :lol:

Return to “Other programming languages”