Score:1

How to get rid of error codes SC1009, SC1073, SC1056, SC1072

vi flag

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 \n 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 '(?<=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;
wjandrea avatar
cn flag
Welcome to Ask Ubuntu! Check out the [tour]. This question doesn't seem to be specific to Ubuntu; really it's about writing Bash code. That's not to say it's off-topic, but there's a site specifically about coding: [so]. **However**, on SO, you'd need to make [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example), which would help catch basic mistakes like this. [Questions caused by a typo get closed there](https://stackoverflow.com/help/on-topic). But, even if you're not posting on SO, the process of making an MRE helps.
Score:4
cn flag

(not an answer to your actual question, this is a reaction to your code)

All bash functions are global, even if they are defined inside another function:

foo() { echo "this is foo"; bar() { echo "this is bar"; }; }

Although bar does not exist until you call foo, bar is not local to foo:

$ bar
bash: bar: command not found
$ foo
this is foo
$ bar
this is bar
Score:4
vn flag

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 \n 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.

uz flag
Jos
I think OP was defining nested functions.
Artur Meinild avatar
vn flag
@Jos Judging from indentation, I don't think so. But added a comment at the end of my answer - only the OP will know.
badprogrammer avatar
vi flag
It fixed. Thank you guys
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.