patternbashMinor
apt alternative for proxy environment with bash shell
Viewed 0 times
alternativeproxywithaptenvironmentshellforbash
Problem
As I am new to bash scripting, and want to use apt-get in my university. I know that many people have issues when trying to do so. My focus is simplicity and ease of use, but still need to be somewhat robust.
My question is: is this code acceptable, or should I improve it? If the answer is not, then please give me some hints.
#!/bin/bash
string="install"
errormsg="\n\tInvalid input\n You should run this script with the following structure:\n\n sudo apt-proxy-install.sh install USERNAME PASSWORD\n\n "
DIRECTORY=~/bin/
if [ "$1" = "$string" ] && { ! ([ -z "$3" ] || [ -z "$3" ]) }
then
if [ ! -d "$DIRECTORY" ]; then
mkdir ~/bin/
fi
printf "#!/bin/bash\nhttp_proxy=""http://$2:$3@10.20.10.50:3128"" sudo apt-get \${@:2}" > ~/bin/apt-proxy
chmod 777 ~/bin/apt-proxy
PATH=~/bin:$PATH
addpath="export $PATH"
sudo cat ~/.bashrc $addpath > ~/.bashrc
else
printf "$errormsg"
fiMy question is: is this code acceptable, or should I improve it? If the answer is not, then please give me some hints.
Solution
Storing passwords in text files is never a good idea, especially if you give that file world-readable permissions.
Try this:
Try this:
#!/bin/bash
mkdir -p ~/bin/
# add ~/bin to PATH if not already there
echo '[[ :"$PATH": == *:"$HOME/bin":* ]] || PATH="$HOME/bin:$PATH"' >> .bashrc
# create the apt-proxy script
cat ~/bin/apt-proxy
#!/bin/bash
stty -echo
printf "Password for %s: " "$LOGNAME"
read password
stty echo
echo
export http_proxy="http://${LOGNAME}:${password}@10.20.10.50:3128"
sudo -S apt-get "$@" <<< "$password"
END_SCRIPT
chmod 755 ~/bin/apt-proxy
cat <<INSTRUCTIONS
The apt-proxy script has been installed.
You may need to log out and log back in before you can use it.
usage: apt-proxy package ...
INSTRUCTIONSmkdir -p dirwill silently do nothing if the directory already exists
- you only need to add the user's
bindir to the PATH if it is not already there
- DO NOT store the user's password. Have the user enter it each time.
- you should not even need to use
sudoto edit files in your home directory.
- 777 permissions is overly generous: the world does not have to be able to edit the file
- use
sudo -Sand pass the user's password via stdin
Code Snippets
#!/bin/bash
mkdir -p ~/bin/
# add ~/bin to PATH if not already there
echo '[[ :"$PATH": == *:"$HOME/bin":* ]] || PATH="$HOME/bin:$PATH"' >> .bashrc
# create the apt-proxy script
cat <<'END_SCRIPT' > ~/bin/apt-proxy
#!/bin/bash
stty -echo
printf "Password for %s: " "$LOGNAME"
read password
stty echo
echo
export http_proxy="http://${LOGNAME}:${password}@10.20.10.50:3128"
sudo -S apt-get "$@" <<< "$password"
END_SCRIPT
chmod 755 ~/bin/apt-proxy
cat <<INSTRUCTIONS
The apt-proxy script has been installed.
You may need to log out and log back in before you can use it.
usage: apt-proxy package ...
INSTRUCTIONSContext
StackExchange Code Review Q#36986, answer score: 3
Revisions (0)
No revisions yet.