In Windows I can write a file containing commands for cmd (usually .cmd
or .bat
files). When I click on those files it will open cmd.exe
and run them commands the file contains.
How would I do this in Ubuntu?
I'm sure this is a duplicate, but I can't find my answer.
Its similar to these questions, but they don't answer the question:
There are two methods.
First, the most common is to write a file, make sure the first line is
Then save the file. Next mark it executable using
chmod +x file
Then when you click (or run the file from the terminal) the commands will be executed. By convention these files usually have no extension, however you can make them end in
.sh
or any other way.A few notes:
/bin/python
,/bin/sh
,/bin/dash
, but even odd ball things work like/bin/mysql
bash file/to/run.sh
A Simple Bash Example
The second method is to record commands using
script
. Runscript
then just do stuff. When you are done doing stuff typeexit
and script will generate a file for you with all the "stuff" you did. This is less used but works quite well for making things like macros.man script
for more info.You mean writing to a file using a shell script? Here are a few ways:
This method will simply create a file, but if the file already exists, it simply changes the modification date to the time you used that command.
That method overwrites the contents of
file
totext
. If you wanted to clear a file, you can simply do this:Say you want to write more than one line to it, and you don't want to use thousands of
echo
commands, you would use this command:That enables you to write multiple lines in one command. The contents of
file
would then be this:If you wanted to append to a file, replace
>
to>>
.Hope this helps!
EDIT: Oh, I see, so you would write the file in gedit, using the
.sh
extension (optional, but it's a good idea), and then on a file manager, right click on the file, select Properties->Permissions, and checkAllow executing file as program
. Then you can double-click on it and it will run :). Also, if you want to do so in the terminal, you can run this command to make it executable (you might want to prependsudo
if it doesn't belong to you):And to run:
The equivalent to Windows batch files is shell scripts, and an excellent getting started guide is Bash Scripting.
For the most part, commands that you can enter on the command line can be placed in a shell script.
A couple of things that are different from Windows batch files:
There are different command interpretors, called shells. The default is bash, but if you are interested, there are others, such as zsh, ksh, dash, perl, python, etc.
To run a shell script, you need to make the file executable, which you can do with
chmod +x <filename>
In Ubuntu, the current directory is not the program search path, so you need to run
./<filename>
, not<filename>
Variable names are
$<varname>
, not%<varname>%
Commands in a shell script are not printed by default, as in a batch file.
The filename's extension can be
.sh
or (more customary) you don't need to use an extension. Put#!/bin/bash
on the very first line of the file, which tells Ubuntu what program to use to run the file.Comments start with
#
, notrem
.Hope this helps and have fun scripting!