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
1k 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
2answers
861 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 ...
16
votes
5answers
5k 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: ...
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, ...
15
votes
6answers
33k 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: ...
14
votes
5answers
1k 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 ...
13
votes
4answers
3k 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
3answers
19k 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 ...
11
votes
5answers
6k 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
2answers
22k 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 # ...
11
votes
4answers
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
1answer
679 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
399 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?
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 ...
8
votes
2answers
20k 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 ...
8
votes
1answer
2k 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
740 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
2k 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 ...
7
votes
2answers
16k 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] ...
7
votes
2answers
347 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 ...
7
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 ...
6
votes
2answers
421 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
49k 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
6
votes
2answers
31k 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
8k 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
103 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
864 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
652 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
2answers
385 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 ...
5
votes
2answers
285 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
121 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
305 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
5k 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
421 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
600 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
398 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
4k 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
1k 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?
4
votes
1answer
7k views

How to change a directory using shell script?

I am trying to change a directory to home/developer. I used cd home/developer in my shell script. After executing script it is again coming to original directory,where I executed shell script.
4
votes
2answers
150 views

Why “./” is used to run “.sh” scripts in Unix? [duplicate]

I am executing a .sh script today. It is executed with prefix "./.sh,I am a bit confuse because it is also executed without ./.but why this is required to use ./ Could you please explain me that why ...
4
votes
3answers
4k 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 ...
4
votes
2answers
273 views

How do I iterate the arguments in a bash script?

Is there any method to iterate the passed arguments in a bash script? This answer from my question shows me how to use each argument, but I want to iterate them because the number of arguments is ...
4
votes
2answers
3k views

How to uppercase first letter of the first word in each sentence using shell script?

I am trying to capitalize each first letter of the first word in each sentence from a txt file called input.txt and I want this input file to be a argument of the shell script ./script.sh input.txt ...
4
votes
1answer
165 views

Help Debugging Backup Script

Every 3 hours the script will run and backup a folder "SOURCE" and it will save it along with the other backups of the current day in the folder "month-day-year" e.g. "03-24-13". When a new day comes, ...
4
votes
1answer
3k views

How to run a command at login?

I need to run the following command (with real parameters) after login: ssh -f user@example.com -L 54321:example.com:54321 -N I need it only for my user, not all users on the machine. Also, I need ...