Executing commands in a series in ubuntu terminal - batch-file

Suppose I have a set of commands to be executed in ubuntu terminal:
for eg:
sudo apt-get update
sudo install xrdp
sudo install java
.....
and many others.
Is it possible in ubuntu to execute the file containing these commands and execute without human intervention (like yes/no) , by default yes?
I tried installing wine , executing $wine cmd /c Commands.bat , after putting all these commands in .bat file but getting errors "Application tried to create a window, but no driver could be loaded."
So, is there any simple way of executing a series of commands in ubuntu?

What you are talking about - multiple commands in one file - is called a "shellscript".
Let's say you have commands.sh:
sudo apt-get update
sudo install xrdp
sudo install java
Then you could execute the commands using:
bash commands.sh
You can also make a directly executable shellscript out of it using the so called shebang mechanism.
Put this line on the very top (line 1) of commands.sh
#!/bin/bash
.. and make the file executable:
chmod +x commands.sh
Now you can execute it directly:
./commands.sh
If you want to say "yes" to all questions, you can use the yes command:
yes | bash "file_with_commands.sh"

No need to do something convolutied and as insane as trying to make cmd.exe work. You already got a perfectly fine working shell on your system. Also you need to understand what programs like sudo do.
sudo is not some magic chant that enables something. sudo is a helper program that allows to start a program with elevated rights. Instead of having a chain of sudo this sudo that you can as just let sudo start a shell and pass a script with all those commands to the shell.
Turning
sudo apt-get update
sudo install xrdp
sudo install java
Into
sudo sh -c 'apt-get update ; install xrdp ; install java'
However that doesn't look right. I think you originally meant to write
sudo apt-get update
sudo apt-get install xrdp
sudo apt-get install java
You can shorten this to
sudo apt-get update
sudo apt-get install xrdp java
Or in coalesced single liner shell
sudo sh -c 'apt-get update ; apt-get install xrdp java'
Now regarding the Yes/No questions. If a program you want to run unattended doesn't offer an option to give a default answer you can make use of the yes program. Even it being named yes it can also answer NO or anything you like:
narfi /home/dw
1007 ~ % yes Hello
Hello
Hello
Hello
Hello
Hello
…
How does this work? There's a concept called the "Standard Input / Output" from which programs read input from and write output to. Shells allow you to redirect the output from one program into the input of another one. This is called pipe-ing. The special character | designates a pipe in shells.
So say you've got a program that wants to see a large number of Aye inputs you could make the following call to the shell
yes Aye | happypirate
So you can combine this with apt-get to give all default yes answer writing
yes | apt-get install …
However this is barking up the wrong tree. Have a look at the call options for apt-get:
~ % apt-get --help
apt 0.9.7.6 for amd64 compiled on Oct 16 2012 18:23:06
Usage: apt-get [options] command
apt-get [options] install|remove pkg1 [pkg2 ...]
apt-get [options] source pkg1 [pkg2 ...]
apt-get is a simple command line interface for downloading and
installing packages. The most frequently used commands are update
and install.
…
-y Assume Yes to all queries and do not prompt <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
…
apt-get gives you an option to auto-answer all yes to all it questions. Neat, right? Note that other programs may use different option keys so you're well advised to always read their fine manuals (RTFM).

At its most basic level, you want to eecute a script. You do that by creating a file like this:
#!/bin/bash
sudo apt-get update
sudo install xrdp
sudo install java
Save that as a file called go, then in the Terminal, you need to make it executable like this:
chmod +x go
Then you can run it by typing:
./go
If your program expects an answer of "yes" for the first question, then "no" for the next question, you can prepare the answers in advance like this and send them into the program:
( echo yes; echo no ) | sudo apt-get update

Related

Cannot upgrade from Ubuntu 18.10 to 19.10

When running the command sudo do-release upgrade I'm facing the following errors:
Checking package manager
Can not upgrade
An upgrade from 'cosmic' to 'eoan' is not supported with this tool.
Since 18.10 no longer supported you can upgrade manually to 19.04
mkdir /tmp/upgrade
cd /tmp/upgrade
wget http://old-releases.ubuntu.com/ubuntu/dists/disco-updates/main/dist-upgrader-all/current/disco.tar.gz
tar -xvf disco.tar.gz
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
/etc/apt/sources.list change all links prefix to -> http://old-releases.ubuntu.com/
sudo python3 dist-upgrade.py
Then upgrade to 19.10
sudo cp /etc/apt/sources.list.bak /etc/apt/sources.list
sudo sed -i -e 's|cosmic|eoan|g' /etc/apt/sources.list
sudo apt update
sudo apt upgrade
I just experienced the whole process of upgrading from 18.10 to 20.04LTS. Key steps are first upgrading from 18.10(cosmic) to 19.04(disco), then 19.10(eoan), finally 20.04LTS(focal).
Upgrade from 18.10 to 19.04. First, we have to manually download disco updates:
mkdir /tmp/upgrade
cd /tmp/upgrade
wget http://old-releases.ubuntu.com/ubuntu/dists/disco-updates/main/dist-upgrader-all/current/disco.tar.gz
tar -xf disco.tar.gz
Now, modify /etc/apt/sources.list(Tip: you'd better save the original sources.list as a backup). This file in Ubuntu 18.10 is like this:
However, "archive.ubuntu.com" has been dead, so we have to replace these links by "old-releases.ubuntu.com", then run dist-upgrade.py in current directory, which is extracted from disco.tar.gz:
sudo sed -i 's/archive.ubuntu.com/old-releases.ubuntu.com/g' /etc/apt/sources.list
sudo sed -i 's/cosmic/disco/g' /etc/apt/sources.list # some answers may forget this
sudo python3 dist-upgrade.py
Note that if you forget to replace "cosmic" with "disco", when running this script, it may throw this dialogue box:
Don't worry, choose "Yes", just like it said, it will update all "cosmic" to "disco" entries.
When this script succeeds, reboot your system and now you have Ubuntu 19.04(disco). Your /etc/apt/sources.list should be like this:
Upgrade from 19.04 to 19.10. Again, replace "disco" to "eoan" in /etc/apt/sources.list. Now you can run apt for updating:
sudo sed -i 's/cosmic/eoan/g' /etc/apt/sources.list
sudo apt update
sudo apt upgrade
If everything goes well, reboot your system and you got 19.10(eoan)! If you just want 19.10, feel free to skip step 3, it's for users who want 20.04 LTS.
Upgrade from 19.10 to 20.04 LTS. This is quite easy, just use Ubuntu Software Updater, it will do everything for you. It took me about 2-3 hours to download and install all updates.

chmod 0644 :Operation not permitted

i wanted to download java using sudo apt on ubuntu 18.04 but it sent me "E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?"
so i tried to run "apt-get update" which sent me lots of error that looks like this " chmod 0644 of file /var/lib/apt/lists/archive.ubuntu.com_ubuntu_dists_bionic_InRelease failed - 201::URIDone (1: Operation not permitted)" so i tried to change the permission using chmod 0644 /var/lib/apt/lists/"
but it didn't work also tried a couple other methods but still didn't work
so hopefully you can tell me what to try to fix it.
btw i'm using windows 10 and running ubuntu 18.04 from the windows store
Remove those files sudo rm -r /var/lib/apt/lists/* and rerun sudo apt-get update

Problems with CakePHP shell in Ubuntu 14.04

could somebody explain why this:
sudo Console/cake
works when I am inside path/to/cakeproject/app while this:
sudo cake
absolutely does NOT work whe I am inside path/to/cakeproject/app/Console?
Actually, understanding this would be simply a first step for me to understand why I can run sudo Console/cake when I am in the right directory (app), but I cannot run sudo cake anywhere even after modifying the PATH variable following these steps. If someone knows any possible causes for this already, please also feel free to share your thoughts.
PS: About when I said "I cannot run sudo cake anywhere", this is the error I get:
sudo: cake: command not found
I had the same problem.
You just need to install "php5-cli" to make it works.
$ sudo apt-get install php5-cli
Then try again with "cake" in your console.
$ cake
It should work.

Brew error: Could not symlink, path is not writable

When I try to install a library with homebrew (brew install aLibrary), I got the following error:
Could not symlink lib/pkgconfig/aFile
/usr/local/lib/pkgconfig is not writable.
What should I do?
There are several questions and answers (1,2,etc.) concerning this brew error, this is an attempt to make a general question as suggested here.
As explained here by Rick:
Start with brew doctor which will show you errors with your brew setup.
You might see something like this: "Warning: /usr/local/lib/pkgconfig isn't writable."
It will give you the advice that: "You should probably chown /usr/local/lib/pkgconfig".
This means: sudo chown -R $(whoami) /usr/local/lib/pkgconfig
Then you will need to link the files with this: brew link yourLibrary
If this does not work hopefully the output of brew doctor will give you enough to continue the search.
Giant Elk had a great suggestion and this is how I fixed my issue, which in my opinion is the cleanest. Users should not change permissions unless they know the ramifications.
Output your installed packages (via brew) to a text file:
brew list > brewlist.txt
Uninstall brew:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"
Re-install brew:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Re-install previous packages (edit list if you do not need all packages):
brew install $(< brewlist.txt )
Use the following.
$ brew doctor
message will display error links to prune. If any found, run next option.
$ brew prune
once these are removed, proceed to link them agian
$ brew link python
I uninstalled brew, re-installed, then the issues went away.
You should simply give the permission to your account by running this command on terminal.
sudo chown -R $(whoami) (path)
In your case:
sudo chown -R $(whoami) lib/pkgconfig/aFile /usr/local/lib/pkgconfig

C - System(""); Execute one at a time

I've got a system("sudo apt-get update | sudo apt-get -y install apache2 zip unzip"); etc, but its doing all the same commands at once?, how do i make it so it does one after the other is finished?, also some may ask the user to enter information from apt-get, how do i allow this to show?
That pipe character (|) means that the output from sudo apt-get update is being piped into the input of sudo apt-get -y install apache2 zip unzip. Although this doesn't actually make any sense, it does mean both get launched at the same time, which is not what you want.
Either replace the single call with two individual system() calls:
system("sudo apt-get update");
system("sudo apt-get -y install apache2 zip unzip");
Note that when you call system(), your program doesn't resume until the process you launched has exited, so this means the first call will execute, then the second.
Or replace the pipe with && (isn't necessarily guaranteed to work, though it really should on any Linux system):
system("sudo apt-get update && sudo apt-get -y install apache2 zip unzip");
Which means the right-hand side of the command will execute only if the left-hand side exits without error (technically, has an exit status of 0).
You can also replace the pipe with a semicolon (;) instead, which should execute both commands in sequence regardless of the exit status of the first command.

Resources