-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript_utils.sh
93 lines (81 loc) · 2.25 KB
/
script_utils.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
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
84
85
86
87
88
89
90
91
92
93
#!/bin/bash
# Script to collect common script functions
# Author: https://github.com/kofaysi/
# Description: This script collects the common script functions. The file is imported in other Nautilus bash scripts.
# Changelog:
# - [2025-01-25]: Initial version
# - [2025-02-08]: Add the DISPLAY variable specifying the graphical session. I
# Function to ensure DISPLAY is set (necessary for GUI tools like Zenity)
function ensure_display() {
if [ -z "$DISPLAY" ]; then
export DISPLAY=:0
fi
}
# Function to handle output
output_message() {
local title="$1"
local text="$2"
if [[ -t 1 ]]; then
echo "$title: $text"
else
case "$title" in
*error*|*Error*|*ERROR*)
dialog_type="--error"
;;
*info*|*Info*|*INFO*)
dialog_type="--info"
;;
*notification*|*Notification*|*NOTIFICATION*)
dialog_type="--notification"
;;
*warning*|*Warning*|*WARNING*)
dialog_type="--warning"
;;
*)
dialog_type="--info" # Default to info dialog
;;
esac
# Display the yad dialog
zenity $dialog_type --text="$text" --title="$title"
fi
}
# Function to check the number of input arguments
check_input_count() {
local expected_count=$1
local comparison=${2:-"-eq"}
shift 2
local actual_count=$#
if ! [ "$actual_count" $comparison "$expected_count" ]; then
output_message "Error" "Expected $comparison $expected_count arguments, but got $actual_count. Please provide the correct number of inputs."
exit 1
fi
}
#!/bin/bash
# Function to check if an item exists
check_item_existence() {
local item="$1"
if [[ -e "$item" ]]; then
return 0 # Item exists
else
echo "File or directory not found: $item"
return 1 # Item does not exist
fi
}
# Function to extract creation timestamp and format it
get_formatted_timestamp() {
local item="$1"
local timestamp=$(stat -c %y "$item")
echo $(date -d "$timestamp" +"%Y-%m-%d %H%M")
}
# Function to get the current date and time
get_current_datetime() {
echo $(date +"%Y-%m-%d %H%M")
}
# Function to rename an item (file or directory)
rename_item() {
local old_name="$1"
local new_name="$2"
mv "$old_name" "$new_name"
echo "Renamed: $old_name -> $new_name"
}
ensure_display