Questions tagged [scripts]

A script is a program or sequence of instructions written in a plain text file. The script interpreter (for example Bash, Python, Perl etc.) reads the file and carries out the instructions as if they had been entered at the command prompt. Use this tag for all questions related to creating, troubleshooting and running scripts.

A script is a series of instructions written in a plain text file. The script interpreter (another installed program) reads the file and carries out the instructions, as opposed to a compiled program which are run directly by the CPU instructions.

A common feature of scripts running on Ubuntu (and other Linux and Unix distributions) is that the first line of the script file can contain a "Shebang" (also known as a "Hashbang"). The Shebang consists of the string #! followed by the path of the interpreter.

For instance, a Bash script will have #!/bin/bash as the Shebang, while a Python script will have #!/usr/bin/python3. By including the Shebang, the correct interpreter will be used when the script file is executed directly.

A Linux system contains many installed scripts by default, especially shell scripts, used to carry out basic tasks, such as package management and updating configurations.

Bash and Dash shells, Awk, Python and Perl are some of the commonly used script interpreters/languages used in Ubuntu. For users, writing scripts makes it possible to achieve complex tasks and automate simple ones.

6741 questions
529
votes
17 answers

How do I run .sh scripts?

Whenever I open a .sh file, it opens it in gedit instead of the terminal. I can't find any option similar to Right Click → Open With → Other Application... → Terminal. How do I open this file in the terminal?
Alex
  • 5,291
498
votes
2 answers

Differences between /bin, /sbin, /usr/bin, /usr/sbin, /usr/local/bin, /usr/local/sbin

I have six directories with command files. These are /bin, /sbin, /usr/bin, /usr/sbin, /usr/local/bin and /usr/local/sbin. What are the differences between these? If I'm writing my own scripts, where should I add them? Related: How to understand…
user125111
425
votes
4 answers

What is the difference between #!/bin/sh and #!/bin/bash?

if I write, #!/bin/bash echo "foo" or #!/bin/sh echo "foo" both yields same. I have seen some scripts starting with #!/bin/sh or #!/bin/bash. Is there any difference between them?
Rahul Virpara
  • 11,860
403
votes
16 answers

How can I get a list of all repositories and PPAs from the command line into an install script?

I know how to list all packages installed on my system. But how could I get a list of all repositories and PPA's into a script that I can run on a new machine to replicate the repository setup including the keys? I know I can look into…
306
votes
7 answers

ssh: automatically accept keys

I've written this small utility script: for h in $SERVER_LIST; do ssh $h "uptime"; done When a new server is added to $SERVER_LIST, the script is stopped with: The authenticity of host 'blah.blah.blah (10.10.10.10)' can't be established. RSA key…
Adam Matan
  • 12,919
237
votes
8 answers

How can I make a script that opens terminal windows and executes commands in them?

I have three scripts I need to run when I start up my Ubuntu machine, they start services I use in my development environment. To do that, I manually open three terminals and type in the commands. Is there any way to create a script that will open…
JSBach
  • 2,551
220
votes
7 answers

How to read complete line in 'for' loop with spaces

I am trying to run a for loop for file and I want to display whole line. But instead its displaying last word only. I want the complete line. for j in `cat ./file_wget_med` do echo $j done result after run: Found. Here is my data: $ cat…
user192118
  • 2,201
219
votes
8 answers

Not able to execute a .sh file: /bin/bash^M: bad interpreter

I wanted to execute a shell script: -rwxr-x--x 1 root root 17234 Jun 6 18:31 create_mgw_3shelf_6xIPNI1P.sh I tried to do a standard procedure, but I got this error: ./create_mgw_3shelf_6xIPNI1P.sh localhost 389 -l…
user165062
  • 2,253
  • 3
  • 15
  • 6
216
votes
11 answers

How can I create a select menu in a shell script?

I'm creating a simple bash script and I want to create a select menu in it, like this: $./script echo "Choose your option:" 1) Option 1 2) Option 2 3) Option 3 4) Quit And according to user's choice, I want different actions to be…
215
votes
11 answers

How to run an alias in a shell script?

I have an executable file mpiexec, whose full path is ~/petsc-3.2-p6/petsc-arch/bin/mpiexec. Since I want to execute this command in different directories (without having to retype the entire path), I setup an alias in my home .bashrc file: alias…
Paul
  • 2,389
197
votes
3 answers

Command to append line to a text file without opening an editor

Assuming i have a line that i want to add to a file without opening an editor. How could i append this line alias list='ls -cl --group-directories-first' to this file config.fish
NES
  • 33,935
193
votes
8 answers

Repeat a command every x interval of time in terminal?

How can I repeat a command every interval of time , so that it will allow me to run commands for checking or monitoring directories ? There is no need for a script, i need just a simple command to be executed in terminal.
user239745
173
votes
5 answers

'chmod u+x' versus 'chmod +x'

What is the difference between chmod u+x and just chmod +x? I have seen a ton of tutorials that say to use u+x to make scripts executable. However, omitting the u doesn't seem to have any effect.
173
votes
2 answers

What is the difference between "source" and "."?

What is the difference between executing a script (e.g. /some/script) with source /some/script and . /some/script in Bash?
binW
  • 13,194
171
votes
11 answers

How can a script check if it's being run as root?

I'm writing a simple bash script, but I need it to check whether it's being run as root or not. I know there's probably a very simple way to do that, but I have no idea how. Just to be clear: What's a simple way to write a script foo.sh, so that the…
Malabarba
  • 10,546
1
2 3
99 100