-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo-shift.sh
executable file
·24 lines (22 loc) · 1.01 KB
/
demo-shift.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/bin/bash
# This script can clean up files that were last accessed over 365 days ago.
USAGE="Usage: $0 dir1 dir2 dir3 ... dirN"
if [ "$#" == "0" ]; then
echo "$USAGE"
exit 1
fi
while (( "$#" )); do
if [[ $(ls "$1") == "" ]]; then
echo "Empty directory, nothing to be done."
else
find "$1" -type f -a -atime +365 -exec rm -i {} \;
fi
shift
done
#A shift statement is typically used when the number of arguments to a command is not known in advance, for
#instance when users can give as many arguments as they like. In such cases, the arguments are usually
#processed in a while loop with a test condition of (( $# )). This condition is true as long as the number of
#arguments is greater than zero. The $1 variable and the shift statement process each argument. The number of
#arguments is reduced each time shift is executed and eventually becomes zero, upon which the while loop
#exits.
#The example below, cleanup.sh, uses shift statements to process each file in the list generated by find: