0

I am trying to write a script which will download git using apt and after successful download of git I want to set the config for git. I wrote a script that does the work. But I am curious to know is there a way to know that the git installation properly done or not. If git installed then i will set config otherwise I will retry or perhaps show an error message.

My Code:

#!/bin/bash
# Read Password
echo -n Sudo Password: 
read -s password

# Run Command

setup_git_config(){
    git config --global alias.co checkout
    git config --global alias.br branch
    git config --global alias.ci commit
    git config --global alias.st status
}

install_git(){
    echo $password | sudo -S apt-get install git -y
    # here i want to check above command successfully ran or not
    setup_git_config
}

#Execute commands
install_git

I am asking this question because I want to write a script to install few essential program and configuration.

Updated:

install_git(){
    typeset ret_code
    ret_code=$?
    echo 'xyz' | sudo -S apt-get install git -y
    if [ $ret_code != 0 ]; then
        printf "Error " $ret_code
        exit $ret_code
    fi
    echo $ret_code #always echo 0
    setup_git_config
}

Always print 0

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
atiq1589
  • 2,227
  • 1
  • 16
  • 24
  • @DidierVanderstoken I tried but ret_code always return 0 :(. I tried with wrong password. turning off network. – atiq1589 Aug 22 '17 at 05:15
  • $? will give you the status of last run command. If the command ran successfully then it will return 0 otherwise it will return 1. You can put an if-else condition based on the returned result. – Abhishek Aug 22 '17 at 05:24
  • Yes as far I understand it @Abhishek . But it always return 0. I think because of pipe, first part echo is ran successfully that's why it return 0 (not sure though). But it wasn't helpful in my case :( – atiq1589 Aug 22 '17 at 05:28

1 Answers1

2

Since you want the return code of apt-get, replace:

ret_code=$?
echo 'xyz' | sudo -S apt-get install git -y

With:

echo 'xyz' | sudo -S apt-get install git -y
ret_code=$?

The return code is available only after the command executes. Consequently, you must capture the value of $? just after apt-get runs, not before it runs.

Notes

  1. apt-get sets it return code to either 0 (success) or 100 (error).

  2. Putting a password in a command, such as echo "$password", means that the password will be available in plain text to anyone on the the machine who can run ps. It would likely be better practice to remove the password and instead add your apt-get command to sudoers with the NOPASSWD option.

John1024
  • 109,961
  • 14
  • 137
  • 171
  • 1
    The password isn't stored in the script, that's not a problem here. But if sudo is just run once, we may just letting it ask for the password. – hek2mgl Aug 22 '17 at 05:34
  • 2
    @hek2mgl Good point. I should clarify: In the 2nd version in the question, we have the command `echo 'xyz' | sudo -S apt-get install git -y` were I presume `xyz` was intended to be the password. In the first version in the question, we have `echo $password | sudo -S apt-get install git -y` which puts the password in `ps` for anyone on the machine to see. – John1024 Aug 22 '17 at 05:38
  • You are right, the second version looks like the password is stored in the script. – hek2mgl Aug 22 '17 at 05:43
  • 1
    2nd part is for showing that wrong password still give ret_code 0. but apparently I am stupid enough to catch ret_code before executing the command. Tanks @John1024 for pointing this out. :) – atiq1589 Aug 22 '17 at 05:52
  • Glad it worked for you. – John1024 Aug 22 '17 at 05:59