Berkeley-Sysadmin DeCal - Lab1a
Berkeley-Sysadmin DeCal - Lab1a
作者学习的版本为spring 2021,但近年的作业内容基本一致
Question 1
1
2
3
4
5
6
curl -s https://raw.githubusercontent.com/0xcf/decal-labs/master/a1/albums.txt | \
grep ', Future' | \
cut -d',' -f1 | \
while read album; \
do mkdir -p "$album"; \
done
Question 2
1
2
3
4
5
6
7
# car.sh
#!/bin/bash
first_dir="${1#/}"
first_dir="${first_dir%%/*}"
echo "$first_dir"
1
2
3
4
# cdr.sh
#!/bin/bash
echo "${1##*/}"
Question 3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# rename.sh
#!/bin/bash
if [ "$#" -ne 3 ]; then
echo "Usage: $0 <directory> <original extension> <new extension>"
exit 1
fi
directory=$1
original=$2
new=$3
if [ "$original" = "$new" ]; then
echo "Original and new extensions are the same. No files to rename."
exit 0
fi
cd "$directory" || { echo "Directory not found"; exit 1; }
for file in *.$original; do
newfile="${file%.$original}.$new"
if [ -f "$file" ]; then
echo "renaming $directory/$file to $directory/$newfile"
mv "$file" "$newfile"
fi
done
Question 4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# mkrandom.sh
#!/bin/bash
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <number of files> <size of each file in bytes>"
exit 1
fi
num=$1
size=$2
for ((i = 1; i <= $num; i++)); do
head -c "$size" /dev/urandom > "$i"
done
This post is licensed under CC BY 4.0 by the author.