In shell scripting, a shell such as bash runs a file containing a list of commands (a script). The script is effectively a program and the shell is its interpreter.

learn more… | top users | synonyms

30
votes
3answers
2k views

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 ...
23
votes
3answers
2k views

Where should I put my script so that I can run it by a direct command?

Where can (should) I put my (bash) script so that it can be used (forever) by terminal or by a direct command: Alt+F2? I know there is /usr/bin and /sbin & /bindirectories but when should I use ...
17
votes
6answers
38k views

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 am got this error: ...
17
votes
5answers
6k views

Run a command with the argument from the last command

When running commands, sometimes maybe you need to run a command with the argument from the last command. How can you do this? Of course, excepting the use of the arrow keys and Del keys: ...
17
votes
2answers
1k views

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 ...
15
votes
6answers
2k views

Renaming hundreds of files at once for proper sorting

I have a ton of files, all named stuff like 1.jpg, 2.jpg, 3.jpg, and so on up to 1439.jpg, however, I have a problem with one of my projects and alphabetizing. It will usually go in the order 1.jpg, ...
14
votes
5answers
2k views

How to display a random line from a text file?

I am trying to write a shell script. The idea is to select a single line at random from text file and display it as an Ubuntu desktop notification. But I want different lines to be selected each time ...
14
votes
3answers
25k views

How to run a script during boot as root

I had a script that automatically enables my wifi without using networkmanager, but I don't know how to run the script as root while the system is booting. How do I make the script run automatically ...
13
votes
4answers
6k views

What does /dev/null mean in a shell script?

I've started learning bash scripting by using this guide: http://www.tldp.org/LDP/abs/abs-guide.pdf However I got stuck at the first script: cd /var/log cat /dev/null > messages cat /dev/null ...
13
votes
1answer
1k views

How to display random jokes from www.icndb.com in your terminal?

I already know that you can get some nice quotes/jokes using the fortune program. But I've found in this urwird script a new source of fun that I'd like to have when I open a new terminal. This time ...
12
votes
2answers
26k views

Why doesn't rc.local run all my commands, and what can I do about it?

I have the following rc.local script: #!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # ...
12
votes
5answers
6k views

How do I limit Dropbox's activity on the hard disk?

On startup, Dropbox runs files indexing, which brings the system pretty much to a stand still for three minutes. Is there a way to restrict Dropbox from accessing the hard disk, or decrease the ...
11
votes
5answers
8k views

What does >> or double Angle brackets mean? [duplicate]

It seems like such a simple questions but I cant find any resource regarding it In C/C++ , it means to store the read in a value from command prompt and store it inside a variable , How about in ...
11
votes
3answers
24k views

How do I run 'sudo' command inside a script?

To do a patch manually I must type this command sudo ./playback_delete_data_patch.sh 09_delete_old_data_p.sql There is a space just before the 09: sudo ./playback_delete_data_patch.sh [space] ...
11
votes
1answer
740 views

Bash comparison and expression operators

I'm new to bash programming. I've read two good, long guides, but they made a mess in my head about operators and keywords. Which are more commonly and widely used? I don't know when to use which ...
10
votes
6answers
3k views

How to get the URL from a file using a shell script

I have a file which consists of a URL. I'm trying to get the URL from that file using a shell script. In the file, the URL is like this: ('URL', 'http://url.com'); I tried to use the following: ...
10
votes
2answers
27k views

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 ...
10
votes
3answers
802 views

How to do: underline, bold, italic, strikethrough, color, background, and size in Gnome Terminal?

How to do: underline, bold, italic, strikethrough, and color in Gnome Terminal? Bold Italic u̲n̲d̲e̲r̲l̲i̲n̲e̲ s̶t̶r̶i̶k̶e̶ ̶i̶t̶ ̶l̶i̶k̶̶e̶ ̶i̶t̶s̶ ̶h̶o̶t Color background font < (its mono ...
10
votes
2answers
411 views

How to run application for a set time in shell

If and How is it possible to, in Terminal, to get an application to run for 30 seconds, before being force closed, and echoing the logs of that process?
10
votes
2answers
61k views

HTTP POST and GET using cURL in linux

I had server application in asp.net in windows in that i had a web service for that . how can i call web service in ubuntu using shell script by using cURL command
10
votes
2answers
224 views

Auto clear search term from lens

There must be a setting or a shell script that would clear the terms from the lens every time it's closed. The behaviour would be: open lens and type a term. reopen the lens with super key. see ...
9
votes
2answers
1k views

What does the first '.' mean in '. ~/.bashrc'? [duplicate]

I saw a post about fix your alias in .bashrc. And he says after you put your alias in .bashrc , you need to use: . ~/.bashrc I do not quite understand what the first dot(' . ') does here. What's ...
9
votes
3answers
4k views

Error message 'source: not found' when running a script

I am creating a little automated setup script that will modify /etc/profile and $HOME/.profile if some paths are not exported. Then, I'd like to reload these automatically. I have read that source ...
9
votes
2answers
3k views

Anyway to make hotkey to turn screen off?

I was just wondering how can one make a script (or make some changes) by which I can turn screen off in Ubuntu(12.04) whenever I want, the way screen can be turned off in Cellphones. And also I would ...
9
votes
2answers
122 views

Run only parts of a script as sudo, entering password only once

I am writing a script (actually a combination of scripts and makefile) that installs external dependencies: some apt-get commands, some git cloning, building, installing, adding users to groups, ... ...
8
votes
1answer
2k views

How do I add a PPA in a shell script without user input?

Basically, I want to run add-apt-repository ppa:(whatever) without the "press Enter to continue" prompt. How would I do this? Essentially, I want to deploy adding a repository in a shell script ...
8
votes
1answer
3k views

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" ...
7
votes
5answers
830 views

How to send a file as an input to another file?

Through the command shell we can redirect the three standard filestreams so that we can get input from either a file or another command instead of from our keyboard, and we can write output and errors ...
7
votes
3answers
1k views

Why do commented lines in a linux configuration file sometimes work? [duplicate]

By a commented line I mean either: #!/usr/bin/perl -w and the comments in /boot/grub/menu.lst file as described in this answer on superuser. It already has a comment, i.e the line begins with a ...
7
votes
3answers
1k views

Difference between ls -l and ll?

I'm relatively new to programming as a whole and some tutorials have been telling me to use ls -l to look at files in a directory and others have been saying ll. I know that ls is a short list, but is ...
7
votes
2answers
453 views

Batch files renaming w/ filename's shift

I have a ton of files named this way: [name]_[phonenumber]_HH-mm-ss_dd-MM-yyyy.mp3. How can I shift [name] and [phonenumber] to the end of file name, and put the date in the beginning in form of ...
6
votes
2answers
699 views

Execute command from file in current shell

I want to executed command from file (which is script) in current shell in terminal. Example: I have file ch_dir contains following: #!/bin/bash cd /usr Now if I execute file usually as following ...
6
votes
2answers
34k views

How to run shell script via terminal using ubuntu 13.04?

I just upgraded to 13.04. Before this when I click on a shell script file it'll give an option to run via terminal but now the option is no longer available and when I click the file it'll directly ...
6
votes
2answers
9k views

Using a variable inside `sed` command

I am using this command to copy certain line from one file to another.Its working fine.No issue with it. sed -f <(sed -e '1,10d; 12,$d; x; s/.*/10a\/;p; x' ../log/file2.txt ) ../log/file4.txt > ...
6
votes
1answer
154 views

Executing a script when inserting a flash drive

How can I write a script that fires on an event? When I insert an USB flash drive, Ubuntu mounts it and automatically opens Nautilus. When this happens with a specific flash drive, I'd like to open a ...
6
votes
1answer
967 views

shell script to conditionally add apt repository

I want to write a shell script that will add an apt repository. I know that I can do it using sudo add-apt-repository -y <repo>. My question is can I do it only if the repository was not ...
5
votes
3answers
721 views

What is the point of the bash Null-operator “:”, colon?

What is the point of the "null" operator in a BASH script? I understand that it is used as a placeholder following an if command when you have nothing to say, but need a command to allow the program ...
5
votes
3answers
6k views

How can I process multi-line records with awk in a bash script?

example.txt is below Restaurant: McDonalds City: Miami State: Florida Address: 123 Biscayne Blvd Phone: 911 Restaurant: 5 guys City: Atlanta State: Georgia Address: 123 Peachtree Rd Phone: 911 ...
5
votes
2answers
444 views

Hiding output of a command

I have a script where it checks whether a package is installed or not and whether the port 8080 is being used by a particular process or not. I am not experienced at all with bash, so I did something ...
5
votes
1answer
127 views

How to get select statement choices into If statements?

I'm very new to programming on Ubuntu. And I'm trying to do a simple bash script. I have a select menu working but I want the choices made in that to go into a number of if statements, corresponding ...
5
votes
1answer
100 views

How to get the tags for the installed software in ubuntu?

I can retrieve the softwares using the dpkg command but is there a command to get the associated category for the software .Like if I have Firefox installed can I some retrieve a tag for it that says ...
5
votes
4answers
336 views

Pulling multiple alternatives from a variable in a case condition

I'd like to use variable as condition in case statement. Something similar to: #!/bin/sh ALLOWED_SERVICES=tomcat6|james; case $1 in $ALLOWED_SERVICES ) service $1 restart ;; * ) ...
5
votes
1answer
6k views

Open Terminal with multiple tabs and execute application

I am new to linux shell scripting. I want to write a shell script which will open terminal with multiple tabs; it should run rtsp client app in each tab. For this, I have gone through question here ...
5
votes
3answers
660 views

Logout scripts: not completed on shutdown, reboot

I'm trying to do some stuff at logout, and the task can take up to 5 minutes to complete. If the user chooses Power off or Reboot directly, the script is killed before completion. If the user just ...
5
votes
3answers
446 views

Convey some message to all users

Is it possible to write a shell script that enables any user (may not be root) to set a message(e.g. "Download on progress do not turn off" etc.) which will be conveyed like pop-up message (zenity or ...
4
votes
8answers
702 views

How to sort by row from terminal

I was looking at this question and I wondered if the following could be done from terminal. I did it in python, just want to see if it could be done from terminal, bash scripting or whatever. ...
4
votes
4answers
441 views

CDIR= $(pwd) gives “Permission denied”

I have a very simple shell script, called deploy.sh #!/bin/sh CDIR= $(pwd) echo Hello World! Unfortunately, running it gives the following error: bird@bird-laptop:~/foo$ sh deploy.sh deploy.sh: ...
4
votes
3answers
6k views

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 ...
4
votes
3answers
302 views

Extract number from string

When using this ubuntu command: ${HOME}/temp/.git describe --always --tags HEAD the output of this command is: v0.1.5-2-p343h3d3 I want to extract 1.5 from the output above and check if it's ...
4
votes
1answer
2k views

How to pause with specific time between two command in script?

I want to do such as following: #!/bin/bash command1 pause 30 seconds command2 exit This is only example script with pause of 30 seconds between run of 2 command. How to do this?