Sunday, April 28, 2024
 Popular · Latest · Hot · Upcoming
4
rated 0 times [  4] [ 0]  / answers: 1 / hits: 552  / 1 Year ago, wed, may 17, 2023, 2:25:17

So I have put in my bash script in shell check and I'm getting this error code which says:
 


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
in front of it.
             ^-- SC1072 (error): Missing '}'. Fix any mentioned problems and try again.

I'm having a hard time getting rid of the error message and finding the problem, I've tried making changes but I'm getting the same error message and I was wondering if any of you could help me find the problem.


    #!/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 '(?<=inets)d+(.d+){3}')

# Get the MAC address
mac_address=$(ip link show $interface | grep -oP '(?<=link/ethers)[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 '(?<=states)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;

More From » bash

 Answers
7

Let's take them one block at a time. First: (SC1009 and 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.

This relates to the function defined in line 61 print_2. Here you're missing a closing bracket } after the function, and before you define the next function.


So I guess it should look like this:


# Define a function to print User management menu
function print_2() {

clear

echo " "
echo "-----User management-----"
echo " "
} # <--- REMEMBER TO ADD THIS

And then: (SC1056 and SC1072)


Line 164:
manage_users;
             ^-- SC1056 (error): Expected a '}'. If you have one, try a ; or
in front of it.
             ^-- SC1072 (error): Missing '}'. Fix any mentioned problems and try again.

I guess these will automatically disappear when you add the aforementioned closing bracket - since the parser is still expecting one.


Also, please be aware that you have the following code at the end of the script:


function print_2() {
true
# other function code goes here
}

This will OVERRIDE any existing print_2 function, so please check if this should be here at all.


Finally, if you're doing nested functions, you should use indentation to show this. I assume the closing bracket should be where I wrote, but only you will know. Indentation will help you manage the different parts in your script, and a general tip would to make indentation consistent for better readability.


[#85] Friday, May 19, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ronicod

Total Points: 71
Total Questions: 111
Total Answers: 111

Location: Montenegro
Member since Fri, Dec 10, 2021
2 Years ago
ronicod questions
Thu, Nov 11, 21, 06:26, 3 Years ago
Sun, May 7, 23, 13:57, 1 Year ago
Sun, Jun 26, 22, 06:13, 2 Years ago
Fri, Oct 14, 22, 13:55, 2 Years ago
Sun, Sep 25, 22, 01:45, 2 Years ago
;