mkdir bug


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting mkdir bug
# 8  
Old 11-24-2010
Glad to know that your issues are gone !!!!!!! !!!
Smilie
# 9  
Old 11-24-2010
Try to avoid duplicate code, the mkdir stuff should be dealt with in the if statement and the copy code should appear once.


Here is your script updated to check return status of mkdir and copy.

Code:
maak_backup ()
{
    echo 'Maken van tijdelijke bestanden, even geduld aub.'
    for i in /home/yannick/* ; 
    do
        cp -r $i $i.bac    
    done
    if [ -d /home/yannick/backup ] ;
    then
        echo 'Backup map = OK!'
    else
        mkdir /home/yannick/backup
        if [ $? -eq 0 ]
        then
            echo 'Bakup map aangemaakt!'
        else
            echo "Failed to create backup directory - aborting backup" >&2
            return 1
        fi
    fi
    echo 'Bezig met kopiëren, even geduld aub.'
    for i in /home/yannick/*.bac ; 
    do
        cp -r $i /home/yannick/backup
        if [ $? -ne 0]
        then
             echo "Copy of $i failed - not removing source" >&2
        else
            echo 'Verwijderen van tijdelijke bestanden, even geduld aub.'
            rm -r $i
        fi
    done
    echo 'Backup geslaagd!'
    return 0
}


Last edited by Chubler_XL; 11-24-2010 at 04:59 PM..
# 10  
Old 11-24-2010
Also, I would suggest that you use double quotes around your variable references or (part of) the script will fail if there are funny file names with spaces in them for example:
Code:
    for i in /home/yannick/* ; 
    do
        cp -r "$i" "$i.bac"    
    done

Some further things to consider that are more related to the method used in the script:
  • there is not return code checking of the above operation so if part of it fails then we end up with a backup where not all files are being copied, but if in the second part those files that have been copied
  • some files and some dirs in the backup dir now have a .bac extension and most do not.
  • the backup dir itself is part of the backup, it should be part of a different tree,
  • the cp command does not preserve access rights
  • I think it is better to use tar, cpio, rsync etc.. so that special files are copied as they are.
  • why /home/yannick/* instead of /home/yannick? For one the hidden directories and files will not be part of the copy operation. Also it is a messier process IMO.
  • What is the function of a double copy? Suppose you stop in mid air. Then the script will still redo the whole operation. Also the script will fail since it cannot create the .bac files and directories an/or there will be .bac.bac files
  • If you ever need to do a restore then files and directories that were deleted long time ago in the original directory will suddenly resurface..
S.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Mkdir

hi linux expert what is a difference between: mkdir test and mkdir ./test and also if ( -e /test ) then and if ( -e ./test ) then thanks in advance Please use icode or code tags next time for your code and data (1 Reply)
Discussion started by: abdossamad2003a
1 Replies

2. UNIX for Dummies Questions & Answers

How to use mkdir{...} with a variable?

Hi, I used "mkdir job{0001..10}" do get directories. Now I want to create directories with a variable, for exemple "mkdir job{0001..$a}". When a=5 I get the directory "job{0001..5}". What have i to change to get job0001,...,job0005 :/ Thx for ur help :) (1 Reply)
Discussion started by: E_taks
1 Replies

3. UNIX for Dummies Questions & Answers

Mkdir utility

Howdy, Puttering around in unix, and read this in the mkdir man page: "The mkdir utility creates the directories named as operands..." What does this mean, i.e. as operands? Many thanks, DN (2 Replies)
Discussion started by: danuke
2 Replies

4. Homework & Coursework Questions

Mkdir

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Do the procedure, which if there are 5 parameters then it creates 4 directories with names of 4 parameters, in... (2 Replies)
Discussion started by: bolones
2 Replies

5. Linux

mkdir command

Hi all, i am new to Linux and need some help. I used the command: mkdir super Directory super is created When i try to change to this directory using: cd /super I get: bash: cd: /super: No such file or directory when i use: rm super I get: rm: cannot remove 'super': Is a directory What... (5 Replies)
Discussion started by: Deside
5 Replies

6. Shell Programming and Scripting

Help me with mkdir command

Hi, please help me with this small script #!/bin/sh curdir=`pwd` n20=$curdir'/n20/' msat=$curdir'/n20/msat/' if then mkdir $n20 fi if then mkdir $msat fi for a in 30 40 50 60 70 80 do (4 Replies)
Discussion started by: Dark2Bright
4 Replies

7. UNIX for Advanced & Expert Users

mkdir

Is there ant way to increase max number of folders in the directory from the 32766: Problem UFS: shell>mkdir mmm mkdir: mmm: Too many links But there are no links, just folders. shell>ls | wc -l 32766 (3 Replies)
Discussion started by: mirusnet
3 Replies

8. Shell Programming and Scripting

mkdir

Hi, I look for a script to create 150 directories : d000 d001 d002 ... ... d149 would you help me please ? I think it would be for i mkdir d$i Many thanks. PS : #uname -a AIX fserver 3 5 0050691A4C00 (2 Replies)
Discussion started by: big123456
2 Replies

9. Linux

install vs mkdir

I'm sitting here late at night, reading linux from scratch, and I come to this part: http://lfs.osuosl.org/lfs/view/stable/chapter06/creatingdirs.html my question to you, the all knowing unix.com'ers is this: what's the difference between using the install command as they do, and using the... (1 Reply)
Discussion started by: thmnetwork
1 Replies
Login or Register to Ask a Question