-
Notifications
You must be signed in to change notification settings - Fork 307
Expand file tree
/
Copy pathnew_function.sh
More file actions
69 lines (53 loc) · 2.07 KB
/
Copy pathnew_function.sh
File metadata and controls
69 lines (53 loc) · 2.07 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
#!/bin/sh
# New function
# Usage: new_function <function_name> [description]
# Example: new_function my_function "This is a test function"
# Function to create a new bash function
function new_function() {
# Check if function name is provided
if [ -z "$1" ]; then
echo "Usage: new_function <function_name> [description]"
return 1
fi
# Get the function name and convert to lowercase
local function_name=$(echo "$1" | tr '[:upper:]' '[:lower:]')
local function_file="$dotfile_dir/functions/${function_name}.sh"
local function_index_file="$dotfile_dir/functions/index.sh"
# Get the description if provided, otherwise use default
local description=${2:-"[Add description here]"}
# Check if file already exists
if [ -f "$function_file" ]; then
echo "Error: Function file '$function_file' already exists"
return 1
fi
# Create alias by taking first letter of each word
local alias_name=$(echo "$function_name" | awk -F'_' '{for(i=1;i<=NF;i++) printf substr($i,1,1)}')
# Create the function file with basic structure
cat > "$function_file" << EOF
#!/bin/sh
# Function: ${function_name}
# Description: ${description}
# Usage: ${function_name} [arguments]
# Alias: ${alias_name}
function ${function_name}() {
# Function implementation goes here
echo "Function ${function_name} called"
}
# Create alias
alias ${alias_name}="${function_name}"
EOF
# Make the file executable
chmod +x "$function_file"
# Add source line to index.sh if it doesn't already exist
if ! grep -q "source \$dotfile_dir/functions/${function_name}.sh" $function_index_file; then
echo "" >> $function_index_file
echo "source \$dotfile_dir/functions/${function_name}.sh" >> $function_index_file
fi
# Open the file in VS Code
cursor "$dotfile_dir/functions/${function_name}.sh"
echo "Created new function file: $function_file"
echo "Added to index.sh and opened in VS Code"
echo "Created alias: ${alias_name} -> ${function_name}"
}
# Alias for new_function
alias nf="new_function"