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.

6665 questions
84
votes
4 answers

Should I save my scripts with the .sh extension?

I have some functional scripts and I want to copy to /usr/bin I want to use them as normal terminal commands. Is it a good practice to use them with the .sh extension or can I save them without extension?
Patterson
  • 1,360
  • 5
  • 16
  • 26
68
votes
4 answers

I've downloaded a .sh file - how do I install this?

I'm new to Ubuntu and know installing programs only from window. It is very easy there: Just double-click the setup.exe and the things start. But how do I install a program on Ubuntu? I want to install something which I couldn't find in the Ubuntu…
cberg
  • 805
39
votes
4 answers

Can scripts run even when they are not set as executable?

I can seem to run scripts (.sh) with and without them being set as executable. So where exactly this matters?
Ashfame
  • 3,214
26
votes
3 answers

Store output of command into the array

This is command: pdc status -a 2>&1 | grep 'okay' It gives the following output [okay ]: you are currently listening: 33 [okay ]: you are currently listening: 22 [okay ]: you are currently listening: 11 I have written this command in…
Prakash V Holkar
  • 2,561
  • 7
  • 22
  • 29
23
votes
4 answers

Reliably check if a package is installed or not

I have a simple requirement. I want to define several variables that will correspond to any number of given packages I want to install via a shell script. Sample code below: MISC="shutter pidgin" WEB="apache2 mongodb" for pkg in $MISC $WEB; do …
vanz
  • 767
21
votes
2 answers

Know whether the shell script contains syntax error without running the script

I have a long shell script containing a lot of conditions and I want to know whether there is any syntax error in the script without running it. Since shell script is interpreted I think it is not possible but is there a way to know by using some…
Ankit Zalani
  • 335
  • 1
  • 3
  • 8
18
votes
1 answer

Script using sed adds an "e" to the output files

I have a script that adds a new user and creates a virtual host for the users domain name. The script works great with one exception... in /etc/apache2/sites-available/ all of my virtual host files have two copies, one with an e and one without. I…
14
votes
1 answer

How do I make post-install scripts?

How do I make post-install scripts so when I reinstall ubuntu everything is as I want it? Things I want to achive: Installing PPA installing my programs themes needs to be installed Settings needs to be changed (power management, short commands,…
Alvar
  • 17,058
14
votes
1 answer

Bash script to move files

I'm a beginner and I need help. I'm trying to make a script to move some files from one directory in another directory. Before to create the script I tested the command and it was working: mv /path/to/source /path/to/destination After I've made…
14
votes
3 answers

Shell Script to Toggle Between Two Commands

I need help creating a shell script to toggle between two commands. When it is run command1 is executed then if it is run again it executes command2 and so on...
era878
  • 2,154
11
votes
4 answers

Environment Variable for Username

What is the Linux equivalent of %username%? I need to run a script on multiple Linux boxes every few days. I made a .sh file, and it works fine. The script deletes some files at a certain path. Problem is that the path includes the username (no, not…
Dr.Ping
  • 123
11
votes
4 answers

Loop variable error in for loop

When I’m using for like for i in 1 2 3 4 5 then my file contains #!/bin/sh at the top. But when I’m using for(( i = 0; i<=5; i++)) then it is showing error Syntax error: Bad for loop variable and running properly when I remove shebang.…
Gaurav Rai
  • 213
  • 1
  • 2
  • 5
10
votes
3 answers

How can I start my application in a more convenient way?

I am new in Ubuntu. I have an application which I open in the following way. I type in the console: cd ~/MyDirectory ./myapp +some arguments How can I find a solution, so I could launch my application without typing these commands in the console…
nick
  • 190
9
votes
4 answers

Finding CPU usage from top command

I'm currently using top -b -d 1 > file1.csv to append the entire output to a csv file. However, I'd like the Cpu(%us) field alone to be entered in file1. I've gotten as far as this: top -b -d 1|grep Cpu where I'm able to view only the Cpu…
P Ramesh
  • 319
8
votes
2 answers

What does if [ $# -lt 2 ] mean?

I'm new to Unix/Linux. I'm trying to understand the code of the developer before me. Can someone please tell me what does this line if [ $# -lt 2 ] mean?
1
2 3
18 19