vendredi 26 juin 2020

In shell, is there a way to make a if statement that looks for anything but nothing, and how to parse arguments?

I am trying to write a shell script that automates the git pushing process; add, commit and push. The full code is here. I want it to work so when I pass the argument -f, I can then follow with my commit message. This is what I have so far:

fast() {
    git add -A
    echo ":: All changes tracked and staged."
    if [ "$2" = "" ]
    then 
        read -p "› Enter commit message: " commit
        echo
        echo "⇓ GIT OUTPUT ⇓"
        git commit -m "$commit"
        echo
    else
        echo "⇓ GIT OUTPUT ⇓"
        git commit -m "$*"
        echo
    fi 
    echo ":: Pushing to remote master."
    echo
    echo "⇓ GIT OUTPUT ⇓"
    git push -u origin master
    echo
    echo "Thanks for using pushall! Exiting..."
    exit
}

The problem I am having currently is that the if statement is not working as expected. No matter what I put after the -f, it always still asks me to enter a commit message. I was trying to make it so if I just ran pushall -f it would ask for a message, but if I ran pushall -f foo then it would commit with message "foo", and not ask.

Also, after I get the if statement working, how would I make the message only $3 and above, not $0, 1, and 2? I put $* just as a placeholder, but I have no idea how I would actually implement that. Any help is appreciated, thanks.

Aucun commentaire:

Enregistrer un commentaire