-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew_script.sh
More file actions
79 lines (73 loc) · 1.85 KB
/
new_script.sh
File metadata and controls
79 lines (73 loc) · 1.85 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/bash
# Function to display help message
show_help() {
echo "Usage: $0 [OPTIONS]"
echo "Options:"
echo " --help, -h Display this help message"
echo " --date, -d Display the current date and time"
echo " --list, -l <path> List files in the specified directory"
echo " --count, -c <file> Count the number of lines in the specified file"
exit 0
}
# Function to display the current date and time
show_date() {
echo "Current date and time: $(date)"
exit 0
}
# Function to list files in a directory
list_files() {
if [ -d "$1" ]; then
echo "Files in directory $1:"
ls -l "$1"
else
echo "Error: $1 is not a valid directory."
exit 1
fi
exit 0
}
# Function to count lines in a file
count_lines() {
if [ -f "$1" ]; then
echo "Number of lines in file $1: $(wc -l < "$1")"
else
echo "Error: $1 is not a valid file."
exit 1
fi
exit 0
}
# Handle command-line arguments
if [ "$#" -eq 0 ]; then
echo "Error: No arguments provided. Use --help or -h for usage information."
exit 1
fi
while [ "$#" -gt 0 ]; do
case "$1" in
--help|-h)
show_help
;;
--date|-d)
show_date
;;
--list|-l)
if [ -z "$2" ]; then
echo "Error: Missing directory path for --list or -l."
exit 1
fi
list_files "$2"
shift
;;
--count|-c)
if [ -z "$2" ]; then
echo "Error: Missing file path for --count or -c."
exit 1
fi
count_lines "$2"
shift
;;
*)
echo "Error: Invalid option $1. Use --help or -h for usage information."
exit 1
;;
esac
shift
done