因此,我将我的 bash 脚本放入 shell 检查中,并收到以下错误代码:
Line 61:
function print_2() {
^-- SC1009 (info): The mentioned syntax error was in this function.
^-- SC1073 (error): Couldn't parse this brace group. Fix to allow more checks.
Line 164:
manage_users;
^-- SC1056 (error): Expected a '}'. If you have one, try a ; or \n in front of it.
^-- SC1072 (error): Missing '}'. Fix any mentioned problems and try again.
我很难摆脱错误消息并找到问题,我尝试进行更改,但收到相同的错误消息,我想知道你们是否可以帮助我找到问题。
#!/usr/bin/bash
# Run script as sudo
if [ "$(id -u)" != "0" ]; then
sudo bash "$0" "$@"
exit
fi
# Define a function to print Network information
function print_1() {
# Clear screen
clear
echo " "
echo "-----Network information-----"
echo " "
# Function that displays the name of the computer, the names of all
# network interfaces (excluding the loopback interface), their IP addresses, MAC
# addresses, gateway addresses, and up/down statuses.
function display_network_info() {
# Get the name of the computer
hostname=$(hostname)
echo "Computer name: $hostname"
# Get the names of all network interfaces (excluding the loopback interface)
interfaces=$(ip link show | grep -v 'LOOPBACK' | grep -oP '(?<=: ).*(?=:)')
# For each network interface, get its IP address, MAC address, gateway address,
# and up/down status
for interface in $interfaces; do
# Get the IP address
ip_address=$(ip addr show $interface | grep -oP '(?<=inet\s)\d+(\.\d+){3}')
# Get the MAC address
mac_address=$(ip link show $interface | grep -oP '(?<=link/ether\s)[0-9a-f]{2}(:[0-9a-f]{2}){5}')
# Get the gateway address
gateway_address=$(ip route | grep -oP '(?<=default via )\d+(\.\d+){3}')
# Get the up/down status
up_down_status=$(ip link show $interface | grep -oP '(?<=state\s)\w+')
# Print the interface information
echo "Interface: $interface"
echo "IP address: $ip_address"
echo "MAC address: $mac_address"
echo "Gateway address: $gateway_address"
echo "Up/down status: $up_down_status"
echo " "
done
}
# Call the display_network_info function
display_network_info
}
# Define a function to print User management menu
function print_2() {
clear
echo " "
echo "-----User management-----"
echo " "
function manage_users() {
# Display the menu options
echo "1. Create user"
echo "2. List users"
echo "3. Change user attributes and password"
echo "4. Remove user"
echo "5. Back to main menu"
echo " "
# Prompt the user to make a selection
read -p "Enter your selection: " selection
# Handle the user's selection
case $selection in
1)
# Prompt the user for the username and password
read -p "Enter the username: " username
read -p "Enter the password: " password
# Create the user
useradd -m $username
echo "$username:$password" | chpasswd
# Display a success message
echo "User $username successfully created"
;;
2)
# Get the names of all users (excluding default and system users)
users=$(getent passwd | grep -vE '^(root|halt|sync|shutdown|daemon|bin|sys|adm|lp|mail|uucp|news|man|proxy|www-data|backup|list|irc|gnats|nobody|systemd-network|systemd-resolve|dbus|avahi|colord|geoclue|pulse|rtkit|sshd|tss|whoopsie|_apt|lxd)' | cut -d: -f1)
# For each user, get their attributes and group membership
for user in $users; do
# Get the user's attributes
attributes=$(getent passwd $user | cut -d: -f5)
groups=$(groups $user)
# Print the user information
echo "User: $user"
echo "Attributes: $attributes"
echo "Groups: $groups"
done
;;
3)
# Prompt the user for the username
read -p "Enter the username: " username
# Check if the user exists
if id -u "$username" > /dev/null 2>&1; then
# Prompt the user for the new password
read -p "Enter the new password: " password
# Set the new password for the user
echo "$username:$password" | chpasswd
# Display a success message
echo "Password for user $username successfully changed"
else
# Display an error message if the user does not exist
echo "Error: User $username does not exist"
fi
;;
4)
# Prompt the user for the username
read -p "Enter the username: " username
# Check if the user exists
if id -u "$username" > /dev/null 2>&1; then
# Remove the user
userdel $username
# Display a success message
echo "User $username successfully removed"
else
# Display an error message if the user does not exist
echo "Error: User $username does not exist"
fi
;;
5)
# Return to the main menu
print_2
;;
*)
# Display an error message for invalid selections
echo "Error: Invalid selection"
manage_users
;;
esac
}
function print_2() {
true
# other function code goes here
}
manage_users;
答案1
让我们一次解决一个问题。首先:(SC1009和SC1073)
Line 61:
function print_2() {
^-- SC1009 (info): The mentioned syntax error was in this function.
^-- SC1073 (error): Couldn't parse this brace group. Fix to allow more checks.
这与第 61 行定义的函数有关。这里,在函数之后、定义下一个函数之前,print_2
缺少一个右括号。}
所以我猜它应该是这样的:
# Define a function to print User management menu
function print_2() {
clear
echo " "
echo "-----User management-----"
echo " "
} # <--- REMEMBER TO ADD THIS
Line 164:
manage_users;
^-- SC1056 (error): Expected a '}'. If you have one, try a ; or \n in front of it.
^-- SC1072 (error): Missing '}'. Fix any mentioned problems and try again.
我猜想当你添加前面提到的右括号时这些会自动消失 - 因为解析器仍然在等待一个。
另请注意,脚本末尾有以下代码:
function print_2() {
true
# other function code goes here
}
这将覆盖任何现有print_2
功能,因此请检查它是否应该在这里。
最后,如果您正在执行嵌套函数,则应使用缩进来显示这一点。我假设结束括号应该在我写的位置,但只有您才知道。缩进将帮助您管理脚本中的不同部分,一般建议使缩进保持一致以提高可读性。
答案2
(这不是对你的实际问题的回答,这是对你的代码的反应)
所有 bash 函数都是全局的,即使它们是在另一个函数内部定义的:
foo() { echo "this is foo"; bar() { echo "this is bar"; }; }
尽管bar
在您调用 之前不存在foo
,但bar
对于 来说却不是本地的foo
:
$ bar
bash: bar: command not found
$ foo
this is foo
$ bar
this is bar