Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug only one command running in if command #19

Closed
gvatsal60 opened this issue Jul 11, 2024 · 2 comments
Closed

Bug only one command running in if command #19

gvatsal60 opened this issue Jul 11, 2024 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@gvatsal60
Copy link
Owner

No description provided.

@gvatsal60 gvatsal60 added the bug Something isn't working label Jul 11, 2024
@gvatsal60 gvatsal60 self-assigned this Jul 11, 2024
Copy link

codeautopilot bot commented Jul 11, 2024

Potential solution

The bug reported suggests that within an if statement, only one command is being executed when multiple commands are expected to run. After analyzing the main.sh script, it appears that all if statements are correctly formatted and should execute multiple commands as expected. Therefore, the issue might be related to how the script is being executed or an external factor such as file permissions or user input handling.

To address this, we should ensure that:

  1. The script is executed with the correct permissions.
  2. The user input is handled correctly.
  3. The script is sourced properly in the shell configuration files.

What is causing this bug?

The bug might be caused by:

  1. Incorrect execution permissions for the script.
  2. Improper handling of user input, leading to unexpected behavior.
  3. The script not being sourced correctly in the shell configuration files.

Code

To ensure the script runs correctly, we can add some debugging statements and make sure the script has the correct permissions. Here is the updated main.sh script with added debugging statements:

#!/bin/bash

# Function to check if the aliases file is sourced in the rc file
check_sourcing() {
    if ! grep -qxF ". \"\$HOME/.aliases.sh\"" "$HOME/$1"; then
        printf "\n# Sourcing custom aliases\n" >> "${HOME}/$1"
        echo ". \"\$HOME/.aliases.sh\"" >> "${HOME}/$1"
    fi

    if [ -r "${HOME}/$1" ]; then
        if ! . "${HOME}/$1"; then
            echo "Error: Unable to source ${HOME}/$1"
        else
            echo "Sourced ${HOME}/$1 successfully."
        fi
    else
        echo "Error: File ${HOME}/$1 does not exist or is not readable."
    fi
}

# Function to check for rc files and source the aliases file
check_rc_files() {
    if [ -f "$HOME/.bashrc" ]; then
        check_sourcing .bashrc
    elif [ -f "$HOME/.zshrc" ]; then
        check_sourcing .zshrc
    elif [ -f "$HOME/.profile" ]; then
        check_sourcing .profile
    else
        echo "Error: rc(bashrc/zshrc/profile) file not found. Please ensure it exists."
    fi
}

# Function to download the aliases file using wget
dw_alias_file_wget() {
    wget -O "$HOME/.aliases.sh" "https://example.com/aliases.sh"
}

# Function to download the aliases file using curl
dw_alias_file_curl() {
    curl -o "$HOME/.aliases.sh" "https://example.com/aliases.sh"
}

# Function to download the aliases file using git
dw_alias_file_git() {
    git clone "https://example.com/aliases.git" "$HOME/.aliases"
    mv "$HOME/.aliases/aliases.sh" "$HOME/.aliases.sh"
    rm -rf "$HOME/.aliases"
}

# Function to download the aliases file
dw_alias_file() {
    if command -v wget >/dev/null 2>&1; then
        dw_alias_file_wget
    elif command -v curl >/dev/null 2>&1; then
        dw_alias_file_curl
    elif command -v git >/dev/null 2>&1; then
        dw_alias_file_git
    else
        echo "Either install wget, curl, or git"
    fi
}

# Main function
main() {
    if [ -f "$HOME/.aliases.sh" ]; then
        printf "File already exists: %s\n" "$HOME/.aliases.sh"
        printf "Do you want to replace it? [y/n]: "
        read -r replace_confirmation
        replace_confirmation="${replace_confirmation:-y}"
        if [ "$replace_confirmation" = "y" ]; then
            printf "\nReplacing %s...\n" "$HOME/.aliases.sh"
            dw_alias_file
        else
            printf "\nKeeping existing file: %s\n" "$HOME/.aliases.sh"
        fi
    else
        dw_alias_file
    fi

    check_rc_files
}

# Ensure the script is executable
chmod +x "$0"

# Run the main function
main

How to replicate the bug

  1. Ensure the script main.sh is saved in your home directory.
  2. Make the script executable: chmod +x ~/main.sh.
  3. Run the script: ~/main.sh.
  4. Follow the prompts and observe if multiple commands within if statements are executed correctly.

If the bug persists, it might be necessary to provide more context or additional files that interact with main.sh.

Task

User input/request

Ticket title: Bug only one command running in if command

Ticket Description: The user reported that within an if statement, only one command is being executed when multiple commands are expected to run.

Click here to create a Pull Request with the proposed solution

Files used for this task:

Changes on main.sh

Bug Analysis Report for main.sh

Overview

The provided script main.sh is designed to manage the sourcing of a custom aliases file (.aliases.sh) into the user's shell configuration files (.bashrc, .zshrc, or .profile). The script includes functions to download the aliases file using wget, curl, or git, and to ensure that the aliases file is sourced in the appropriate shell configuration file.

Issue Description

The user reported a bug with the title "Bug only one command running in if command." This suggests that within an if statement, only one command is being executed when multiple commands are expected to run.

Analysis

The script contains several if statements that need to be inspected to ensure that multiple commands within the if block are executed correctly. Here is a detailed analysis of each relevant if statement:

1. check_sourcing Function

if ! grep -qxF ". \"\$HOME/.aliases.sh\"" "$HOME/$1"; then
    printf "\n# Sourcing custom aliases\n" >> "${HOME}/$1"
    echo ". \"\$HOME/.aliases.sh\"" >> "${HOME}/$1"
fi
  • Analysis: This if statement checks if the .aliases.sh file is already sourced in the specified rc file. If not, it appends the sourcing command to the rc file. The commands within the if block are correctly terminated with newlines, so they should execute as expected.
if [ -r "${HOME}/$1" ]; then
    if ! . "${HOME}/$1"; then
        echo "Error: Unable to source ${HOME}/$1"
    else
        echo "Sourced ${HOME}/$1 successfully."
    fi
else
    echo "Error: File ${HOME}/$1 does not exist or is not readable."
fi
  • Analysis: This nested if statement checks if the rc file is readable and attempts to source it. If sourcing fails, it prints an error message. The commands are correctly terminated with newlines, so they should execute as expected.

2. check_rc_files Function

if [ -f "$HOME/.bashrc" ]; then
    check_sourcing .bashrc
elif [ -f "$HOME/.zshrc" ]; then
    check_sourcing .zshrc
elif [ -f "$HOME/.profile" ]; then
    check_sourcing .profile
else
    echo "Error: rc(bashrc/zshrc/profile) file not found. Please ensure it exists."
fi
  • Analysis: This if-elif-else statement checks for the existence of various rc files and calls the check_sourcing function accordingly. The commands are correctly terminated with newlines, so they should execute as expected.

3. dw_alias_file Function

if command -v wget >/dev/null 2>&1; then
    dw_alias_file_wget
elif command -v curl >/dev/null 2>&1; then
    dw_alias_file_curl
elif command -v git >/dev/null 2>&1; then
    dw_alias_file_git
else
    echo "Either install wget, curl, or git"
fi
  • Analysis: This if-elif-else statement checks for the availability of wget, curl, or git and calls the respective function to download the aliases file. The commands are correctly terminated with newlines, so they should execute as expected.

4. main Function

if [ -f "$HOME/.aliases.sh" ]; then
    printf "File already exists: %s\n" "$HOME/.aliases.sh"
    printf "Do you want to replace it? [y/n]: "
    read -r replace_confirmation
    replace_confirmation="${replace_confirmation:-y}"
    if [ "$replace_confirmation" = "y" ]; then
        printf "\nReplacing %s...\n" "$HOME/.aliases.sh"
        dw_alias_file
    else
        printf "\nKeeping existing file: %s\n" "$HOME/.aliases.sh"
    fi
else
    dw_alias_file
fi
  • Analysis: This if statement checks if the .aliases.sh file already exists and prompts the user for confirmation to replace it. The commands within the if block are correctly terminated with newlines, so they should execute as expected.

Conclusion

Upon inspecting the if statements in the main.sh file, all commands within the if blocks are correctly terminated with newlines or semicolons, ensuring that multiple commands are executed as expected. There are no syntax issues that would cause only one command to run within an if block.

If the bug persists, it may be necessary to look into other files or consider runtime issues such as file permissions, user input handling, or external dependencies.

Disclaimer: This comment was entirely generated using AI. Be aware that the information provided may be incorrect.

Current plan usage: 65.88%

Have feedback or need help?
Discord
Documentation
[email protected]

@gvatsal60
Copy link
Owner Author

PR: #20

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant