Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple bash scripts
#1
A simple script for installing programs in the terminal:

#!/bin/bash
echo "Enter the name of the program you want to install"
read progName
sudo apt-get install $progName
echo "The program has been installed!"
sudo apt-get update

A program to uninstall programs:

#!/bin/bash
#Uninstall programs from the terminal
echo "What is the program you want to uninstall?"
read program_name
sudo apt remove $program_name
echo "The program was uninstalled!"
sudo apt-get autoremove

If you find a better or more efficient way, feel free to change the script. I wish your opinion.Thank you!
Reply
#2
(07-15-2019, 06:18 PM)Daniel2213 Wrote: If you find a better or more efficient way, feel free to change the script.

Interactive scripts belong more into the domain of DOS than of UNIX.

Code:
#!/bin/sh
sudo apt update
sudo apt install $*


You can also adapt it to fit into your bashrc, so you don't need an extra file for this.

Code:
function install
{
   sudo apt update
   sudo apt install $*
}


If you really want to print a status message at the end, you should check the return
value to see if the operation was successfull.

Code:
function install
{
   sudo apt update
   sudo apt install $*
   [[ "$?" -eq 0 ]] && echo "Success" || echo "Operation failed"
}
My website - My git repos

"Things are only impossible until they’re not." - Captain Jean-Luc Picard
Reply
#3
I need help with this script. I do not understand why they give me these errors:
1. ./if: line 10: syntax error near unexpected token `}'
2. ./if: line 10: `else{echo "Eror! Try again";}'
Can you help me please?!

#!/bin/bash
echo "Program name"
read prog
echo "Want to install it? If you do ,press 'y' if you do not ,press 'n'"
read daSauNu
if($daSauNu==y)
sudo apt install $prog;
if($daSaunu==n)
echo "Setup has been canceled";
else{echo "Eror! Try again";}
Reply
#4
(07-19-2019, 07:00 PM)Daniel2213 Wrote: You are using the wrong syntax for Bash “if” statements.  Do an Internet search for “bash if statement tutorial” or read the bash man pages for more Information on how to use it.  You could do the same for the “read” statement, which has a -p ”prompt” option, so that you would not need to do a separate “echo” before the “read”.

(07-19-2019, 07:00 PM)Daniel2213 Wrote: You are using the wrong syntax for Bash “if” statements.  Do an Internet search for “bash if statement tutorial” or read the bash man pages for more Information on how to use it.  You could do the same for the “read” statement, which has a -p ”prompt” option, so that you would not need to do a separate “echo” before the “read”.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)