A script is a sequence of commands or instructions that are executed by an interpreter program rather than compiled into a standalone executable program.

learn more… | top users | synonyms (1)

158
votes
22answers
83k views

Repeat a Unix command every x seconds forever

There's a builtin Unix command repeat whose first argument is the number of times to repeat a command, where the command (with any arguments) is specified by the remaining arguments to repeat. For ...
57
votes
5answers
32k views

Allow setuid on shell scripts

The setuid permission bit tells Linux to run a program with the effective user id of the owner instead of the executor: > cat setuid-test.c #include <stdio.h> #include <unistd.h> int ...
34
votes
8answers
46k views

Looping through files with spaces in the names?

I wrote the following script to diff the outputs of two directores with all the same files in them as such: #!/bin/bash for file in `find . -name "*.csv"` do echo "file = $file"; diff ...
27
votes
7answers
1k views

What's the equivalent to && when writing a bash script?

I apologize in advance if this is a duplicate question. I did make an effort to search/check before asking here. I'm comfortable with writing one-liners like this: foocommand && ...
24
votes
6answers
5k views

What Unix commands can be used as a semaphore/lock?

I want to run multiple Bash shell scripts in parallel. However, I want to avoid race conditions. What Unix commands are truly atomic that I could use for this purpose, and how can I use them?
20
votes
2answers
665 views

How do I move all files output from a command?

I have this grep command to find files without the word Attachments in them. grep -L -- Attachments * I want to move all the files that are output from that command. How do I do that in bash? Do I ...
20
votes
1answer
876 views

How to implement a horizontal cat? [duplicate]

Standard cat concatenates files line by line (row by row, if you will). I find myself needing a horizontal cat command more and more often recently; i.e. a command that takes a list of files and ...
20
votes
1answer
581 views

How to move a directory, file by file? (instead of “copy then remove”)

My computer has one 500GB drive. I want to move 400GB of data from /unencrypted to /encrypted. Both directories are on the same partition, but /encrypted is handled by ecryptfs, so mv /uncrypted/* ...
19
votes
5answers
8k views

Using while loop to ssh to multiple servers

I have a file servers.txt, with list of servers: server1.mydomain.com server2.mydomain.com server3.mydomain.com when I read the file line by line with while and echo each line, all works as ...
19
votes
4answers
7k views

How do I exit a script in a conditional statement?

I'm writing a bash script where I want to exit if the user is not root. The conditional works fine, but the script does not exit. [[ `id -u` == 0 ]] || (echo "Must be root to run script"; exit) ...
19
votes
3answers
7k views

What does it mean to have a $“dollarsign-prefixed string” in a script?

I just saw this in an init script: echo $"Stopping Apache" What is that dollar-sign for? My research so far: I found this in the bash manual: extquote If set, $'string' and ...
19
votes
5answers
786 views

shebang and path

Why does a shebang need a path? Wrong #!ruby Correct #!/usr/local/bin/ruby #!/usr/bin/env ruby The operating system should have the information regarding the path for a registered command, and ...
18
votes
3answers
2k views

Does the shebang determine the shell which runs the script?

This may be a silly question, but I ask it still. If I have declared a shebang #!/bin/bash in the beginning of my_shell_script.sh, so do I always have to invoke this script using bash ...
17
votes
8answers
2k views

The most universal scripting language for Linux is?

We are writing scripts for Linux systems, there has been some debate over what would be the most universally Linux present scripting language to use. Bash, SH, Posix? What?
17
votes
8answers
6k views

Object-oriented shell for *nix

Preface: I love bash and have no intention of starting any sort of argument or holy-war, and hopefully this is not an extremely naive question. This question is somewhat related to this post on ...
17
votes
4answers
2k views

Universal Node.js shebang?

Node.js is very popular these days and I've been writing some scripts on it. Unfortunately, compatibility is a problem. Officially, the Node.js interpreter is supposed to be called node, but Debian ...
16
votes
7answers
11k views

Using xargs with input from a file

Say I have a file with the following bob john sue Now these directly corrospond to (in this case) URL pattern such as http://example.com/persons/bob.tar, john.tar, sue.tar. I would like to take ...
16
votes
4answers
468 views

Which folder should I write to for my intermediate files, as a dev?

I'm starting to write a few bash scripts for my project and they need some intermediate files or variables to be written. I want to know which folders can I be sure of having access to when someone ...
14
votes
7answers
30k views

Execute remote commands, completely detaching from the ssh connection

I have 2 computers, localpc and remoteserver. I need localpc to execute some commands on remoteserver. One of the things it needs to do is start a backup script that runs for a number of hours. I ...
13
votes
7answers
8k views

Faster way than ping for checking if computer online?

I'm writing a wake on lan script for a set of our lab computers. We have sqlite db with a list of the computer hostnames, IPs, and MACs and currently I ping each of them with '-c1' so it doesn't run ...
13
votes
1answer
1k views

Shebang line with `#!/usr/bin/env command --argument` fails on Linux

I've got a simple script: #!/usr/bin/env ruby --verbose # script.rb puts "hi" On my OSX box, it runs fine: osx% ./script.rb hi However, on my linux box, it throws an error linux% ./script.rb ...
12
votes
3answers
1k views

How to make those old-school GUIs (like the debian/arch installer)

I've been doing a lot of installs lately, and when possible I usually opt for the text install from the Live CD's GRUB menu. Most installers looks like this: I always thought these GUIs (if you can ...
12
votes
2answers
6k views

Confusing use of && and || operators

I was skimming through an /etc/rc.d/init.d/sendmail file (I know this is hardly ever used, but I'm studying for an exam), and I've become a bit confused about the && and the || operators. ...
12
votes
7answers
2k views

Practical tasks to learn shell scripting

I'm looking for some common problems in unix system administration and ways that shell scripting can solve them. Completely for self-educational purposes. Also I'd like to know how would you go about ...
12
votes
3answers
6k views

Answer yes in a bash script

I'm trying to do a git clone trough a bash script, but the first time that I run the script and the server is not known yet the script fails. I have something like this: yes | git clone ...
12
votes
3answers
363 views

When to use redirection to stderr in shell scripts

I know that well-behaved utilities like grep output "normal" messages to stdout, and error messages to stderr. $ grep '^foo' file1 file2 file1:foo grep: file2: No such file or directory When I'm ...
11
votes
4answers
11k views

How can I use bash's if test and find commands together?

I have a directory with crash logs, and I'd like to use a conditional statement in a bash script based on a find command. The log files are stored in this format: /var/log/crashes/app-2012-08-28.log ...
11
votes
1answer
3k views

Making a progressbar with “dialog” from rsync output

I'm looking for a way to filter/redirect rsync output in a manner where it can be fed to the "dialog --gauge" command, so I can get a nice looking progressbar during file sync. Currently I have only ...
10
votes
4answers
9k views

Script to change current directory (cd, pwd)

I want to run a script to simply change the current working directory: #!/bin/bash cd web/www/project But, after I run it, the current pwd remains unchanged! How can I do that?
10
votes
3answers
3k views

How to “send” variable to sub-shell?

I run the following script: VAR="Test" sh -c 'echo "Hello $VAR"' But I get : # ./test.sh Hello How can I "send" the variable VAR of my script to the shell created with sh -c?
10
votes
2answers
3k views

Regular expression in bash script

This is my first time bash scripting so I'm probably making an easy mistake. Basically, I'm trying to write a script that gets the groups of a user, and if they are in a certain group, it will log ...
10
votes
6answers
4k views

What are good online resources for learning shell scripting? [closed]

I am a beginner to shell scripting. I have not yet hardly coded a single program, but I am interested in learning it completely. Can anyone suggest some good online resources which will guide me?
9
votes
3answers
15k views

File extensions for unix shell scripts

On wikipedia, the article for .sh says: For the .sh file extension type, see Bourne shell. How about other unix shells? I know that the shebang is used inside the file to indicate an ...
9
votes
6answers
836 views

Text Manipulation Across multiple lines

I Have a file that has text like this: AAAA BBBB CCCC DDDD 1234 5678 9012 3456 EEEE 7890 etc... And i want to match up the Alphabetic lines with the Numeric lines so they are like this: ...
9
votes
5answers
5k views

Run a script via cron every other week

I've done quite a bit of research in how to do this, and I see there's no direct way in cron to run a job, say, every other Thursday. Right now, I'm leaning toward making a script that will just run ...
9
votes
3answers
650 views

How to pipe the list of commands displayed by of “tab complete”?

When using commands in bash I like the double tab option to display the available commands. Some commands have more possible matches than others: Is there a way I can pipe the output of the double ...
9
votes
3answers
1k views

Advice for managing SSH keys

What is the best practice you have found for managing lots of SSH keypairs? I use SSH to connect to several systems, both at home and at work. I currently have a fairly small, manageable collection ...
9
votes
1answer
2k views

How do I make a shell script that sends output to a process

I'm currently running a server console program in a screen because I need to both read it and occasionally send commands. I'd like to run the app as a deamon in the background (start/stop it with ...
9
votes
3answers
570 views

Send email written in markdown using mutt

Sometimes I need to send a fragment of code on google-group inline. Text does not help here; I can type it in markdown, convert it to html (using pandoc etc.), attach to mutt as text/html and send it. ...
8
votes
5answers
1k views

How can I have more than one possibility in a script's shebang line?

I'm in a bit of an interesting situation where I have a Python script that can theoretically be run by a variety of users with a variety of environments (and PATHs) and on a variety of Linux systems. ...
8
votes
6answers
748 views

Best way run a command on each file in a directory tree

There appear be a number of ways to do this including loops in shell script, find and xargs. Which of these is best, and which is most portable?
8
votes
4answers
7k views

grep — removing text after delimiter token

I have a file in which I need to eliminate everything after the first ; on every line. So a file like this: sdfsdsdf; fsdfsddf;sdfsd; Will result in this: sdfsdsdf fsdfsddf I have looked into ...
8
votes
2answers
7k views

Invert boolean variable

I want to try simple script flag=false while !$flag do read x if [ "$x" -eq "true" ] then flag=true fi echo "${x} : ${flag}" done But when i run it, if i type true, i will see ...
8
votes
3answers
3k views

End sox recording once silence is detected

I'm writing a script that uses sox to record me talking. Now I need sox to wait until it detects sound before it begins recording, and I do have that figured out. But I also need sox to exit once ...
8
votes
4answers
303 views

Do you prefer bash scripts or aliases for shortcuts?

I have a ~/bin directory (which is on my PATH) where I store a lot of little 1 or 2 line scripts. Some of them just cd into a directory and run a command on a file, like vim or something. But I also ...
8
votes
3answers
409 views

rsync most recent x GB

I'm looking for a command/script to allow the most recently modified files (up to) 10GB be copied to another computer. So if there are 4 files 4 GB each, only 2 of them should be transferred by the ...
8
votes
1answer
403 views

Command to move a window to all desktops

I'd like to be able to move (pin) an existing window to all desktops using a scriptable command, rather than through the window menu. wmctrl -r <WIN> -t <DESK> will move a window to a ...
8
votes
1answer
785 views

python == python2 OR python == python3 ? How to package, distribute python py2k scripts?

Depending on system, python==python2 or python==python3. Executable Python scripts, starts with: #!/usr/bin/env python #!/usr/bin/env python2 #!/usr/bin/env python3... For python py3k it is ...
7
votes
8answers
7k views

Automatically run commands over SSH on many servers

There is a list of IP addresses in a .txt file, ex.: 1.1.1.1 2.2.2.2 3.3.3.3 Behind every IP address there is a server, and on every server there is an sshd running on port 22. Not every server is ...
7
votes
3answers
5k views

What is IFS in context of for looping?

I'm learning bash scripting right now. I was reading this thread: How to loop over the lines of a file? What is IFS? With for and IFS: