-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathtouchx
More file actions
executable file
·83 lines (67 loc) · 1.58 KB
/
touchx
File metadata and controls
executable file
·83 lines (67 loc) · 1.58 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
80
81
82
83
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_NAME=$(basename "$0")
usage() {
local exit_code=${1:-0}
cat <<EOF
Usage: $SCRIPT_NAME [OPTIONS] <file> [<file> ...]
Create or update the specified file(s) and make them executable.
Arguments:
<file> [<file> ...] One or more files to create or update.
Options:
-h, --help Show this help message and exit.
If a specified file does not exist, it will be created.
If it exists, its timestamp will be updated.
All specified files will be given executable permissions.
Examples:
$SCRIPT_NAME script.sh
$SCRIPT_NAME file1.sh file2.sh
EOF
exit "$exit_code"
}
# Check if no arguments were provided
if [ "$#" -lt 1 ]; then
usage 1
fi
# Collect file arguments
files=()
# Parse options and collect valid file arguments
while [[ $# -gt 0 ]]; do
case "$1" in
-h | --help)
usage
;;
-*)
echo "Error: Unknown option '$1'" >&2
usage 1
;;
*)
files+=("$1")
;;
esac
shift
done
# Ensure at least one valid file argument remains
if [ "${#files[@]}" -eq 0 ]; then
echo "Error: No files specified." >&2
usage 1
fi
# Process each file argument
for file in "${files[@]}"; do
# Ensure the file argument is not empty
if [[ -z $file || $file =~ ^[[:space:]]+$ ]]; then
echo "Error: Invalid filename '$file'." >&2
exit 1
fi
# Create or update the file
if ! touch "$file"; then
echo "Error: Failed to create or update '$file'." >&2
exit 1
fi
# Make the file executable
if ! chmod +x "$file"; then
echo "Error: Failed to set execute permission for '$file'." >&2
exit 1
fi
echo "Created and made executable: '$file'"
done