46887

Executing commands in a series in ubuntu terminal

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?

Answer1:

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 <hr>

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 <hr>

If you want to say "yes" to all questions, you can use the yes command:

yes | bash "file_with_commands.sh"

Answer2:

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).

Answer3:

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

Recommend

  • PE file - what's missing?
  • Passing a Scala type to a function
  • Pass nested C++ vector as built-in style multi-dimensional array
  • C++11 variable size POD struct
  • Ruby: FileUtils.cp truncates file; FileUtils.mv it does not?
  • Generic/Unknown HTTP Error with response code 0 using UnityWebRequest
  • Java Application vs. Java Desktop Application in Netbeans [duplicate]
  • why calling cd shell command through system() or execvp() from a child process won't work?
  • Installing Perl6 and Panda on Ubuntu 15.10. Problems with bootstrap.pl
  • How can I get the full list of running processes on a Mac from a python app
  • import scipy.sparse failed
  • aapt.exe'' finished with non-zero exit value 1
  • Differences in dis-assembled C code of GCC and Borland?
  • Android application: how to use the camera and grab the image bytes?
  • Query to find the duplicates between the name and number in table
  • Tamper-proof configuration files in .NET?
  • Code in Job's Script Block after Start-Process Does not Execute
  • Eloquent update method change created_at timestamp
  • Installing Apache MyFaces 2 on WildFly 8.2.0
  • Spark fat jar to run multiple versions on YARN
  • Using jQuery closest() method with class selector
  • Array.prototype.includes - not transformed with babel
  • Sony Xperia Z Tablet not found by adb
  • How to recover from a Spring Social ExpiredAuthorizationException
  • Running a C# exe file
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • How can I estimate amount of memory left with calling System.gc()?
  • R: gsub and capture
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Comma separated Values
  • Matrix multiplication with MKL
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper folder structure for lots of source files
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • Hits per day in Google Big Query
  • File not found error Google Drive API
  • How does Linux kernel interrupt the application?
  • Converting MP3 duration time
  • How to load view controller without button in storyboard?