# SHELL - Programing



## julia1997 (4. Apr 2018)

Hallo! Könntet mir bitte jemand meinen Code kontrollieren? Ich bin noch nicht so vertraut mit Bash-Programming und hätte daher gerne etwas Sicherheit bevor ich abgebe.

Aufgabe:  Write a script my_back_up.sh to perform a back up of the current directory into a given folder. The script receives a single parameter which is the path of the folder where the back up is to be stored. If the back up folder already exists then a file needs to be copy if and only if it does not exist within the back up folder or if it is newer than the existing one in the back up folder.



```
#!/bin/bash
#Sheet2
#Task2
#

backup_dir=$1'/backup'

if [ ! -d $backup_dir ]
#only if backup does not exist
then
    #crates new backup directory
    mkdir -p $backup_dir
    #copies all files from current directory
    #$PWD gives the current directory path of the script
    cp -r $PWD'/' $backup_dir
exit 1
elif [ -d $backup_dir ]
    #only if backup already exists
    then
    #iterates over all files in current directory
    for file in $PWD'/'
        do
        if [ $file -nt $backup_dir'/$file' ]
        #overrides a file in backup directory if its newer
        then
            cp -r $file $backup_dir
        elif [ ! -a $backup_dir'/$file' ]
        #copies a file if it is not existing in backup directory
        then
            cp -r $file §backup_dir
        fi
    done
fi
```


----------



## AndyJ (5. Apr 2018)

Lass das script doch einfach laufen um zu sehen ob es funktioniert. Wenn du Probleme hast, starte dein Script so:

```
$ bash -xv my_back_up.sh
```

-x schaltet Tracing ein und
-v sorgt fuer ausfuehrliche Messages

Alternativ kannst du im Script auch

set -xv

schreiben, das hat den gleichen Effect.

Cheers,
Andy


----------

