A shell script is a script written for the shell, or command line interpreter, of an operating system.

learn more… | top users | synonyms (1)

29
votes
3answers
28k views

What does export do in BASH? [duplicate]

Possible Duplicate: Difference between “a=b” and “export a=b” in bash It is hard to admit, but I have never really understood what exactly export does to an ...
23
votes
6answers
63k views

looping through `ls` results in bash shell script

Does any one have a template shell script for doing something with ls for a list of directory names and looping through each one and doing something? I'm planning to do ls -1d */ to get the list of ...
18
votes
10answers
185k views

How to execute .sh file on Windows?

When I am trying to execute a file(name.sh) in the command line by the command ./name.sh , I am getting the error that: "." is not recognized as an internal or external command, operable or ...
14
votes
10answers
53k views

Populating Array in DOS Batch Script

How can I setup an array variable in a DOS batch script? I would like to load it with a list of file names to process. I really would like to keep this as simple as possible. Thank you.
14
votes
4answers
9k views

Should I put *.sh and *.rb file extensions on all my scripts?

I have a bunch of hand-rolled executable scripts in my $HOME/bin directory. Some are written in bash, some in Ruby. All of them have the shebang line at the top telling the shell what interpreter to ...
13
votes
2answers
59k views

How do I concatenate strings in a bash script?

How can I concatenate strings and variables in a shell script? stringOne = "foo" stringTwo = "anythingButBar" stringThree = "? and ?" I want to output "foo and anythingButBar"
13
votes
3answers
5k views

Escaping apostrophe in single quotes string in bash

I don't understand how Bash evaluates escaping of apostrophe characters in single quoted strings. $ echo ''\''Hello World'\''' 'Hello World' #works $ echo '\'Hello World\'' > # expects you to ...
12
votes
5answers
7k views

Log rotation of stdout?

I have a Linux program which can write information to stdout and stderr. I have a shell script which redirects that output to a file in /var/log. (Via >> and 2>&1.) Is there a way to ...
11
votes
3answers
5k views

How do I launch a bash script from Finder in OS X?

How do I launch a bash script from Finder in OS X? I'm OK working at the CLI, but my little script is not so loved by my GUI-bound colleagues.
10
votes
2answers
8k views

Avoid unwanted path in Zip file

I'm making a shell script to package some files. I'm zipping a directory like this: zip -r /Users/me/development/something/out.zip /Users/me/development/something/folder/ The problem is that the ...
10
votes
3answers
11k views

Determine the architecture of a Mac from the command line or script?

I'm writing a shell script, and I need to know the architecture, i.e. PPC or Intel. Back in the day, there was a program /bin/arch that told you, but my mac doesn't seem to have it... Is there an easy ...
10
votes
3answers
7k views

Bash shell: list all files of type .png or .PNG?

In a bash script, how can I say 'for all files of type .png or .PNG'? I'm trying : for i in (`ls *.PNG` && `ls *.png`) but getting a syntax error.
10
votes
2answers
9k views

Programmatically open tab in gnome-terminal, execute command, and have tab stay open

I am trying to write a command that will launch a few terminal tabs, execute something in each tab, and have each tab stay open after the command in finished, so I can look at the output and type more ...
9
votes
3answers
42k views

How to execute a shell script on startup?

I have created a script to start a server (my first question). Now I want it to run on the system boot and start the defined server. What should I do to get this done? My findings tell me put this ...
9
votes
3answers
8k views

Passing two arguments to a command using pipes

Usually, we only need to pass one argument: echo abc | cat echo abc | cat some_file - echo abc | cat - some_file Is there a way to pass two arguments? Something like {echo abc , echo xyz} | cat ...
8
votes
11answers
9k views

how to `tail` the latest file in a directory

In shell, how can I tail the latest file created in a directory?
8
votes
3answers
618 views

How can I run my python program directly from the shell?

I wrote a script. When it starts, it won't stop, and it keeps getting data from the Internet. I can call it this way: cd /User/Desktop/project/internetScanner/ python3 main.py start But I would ...
8
votes
6answers
55k views

ssh and shell through ssh : how to exit?

I'm launching a distant script through SSH like this: ssh user@ipaddress '~/my_script.sh' Everything is going fine but once the script is finished, the connection is not closed. I Have to press ...
8
votes
5answers
5k views

Run script in OS X 10.6 on network connection. (like /etc/network/if-up.d/)

Is there any way to run a script when a network interface comes up in Mac OS X? I've tried putting a script named ip-up in /etc/ppp but it doesn't seem to fire when I disconnect and reconnect to a ...
7
votes
2answers
677 views

How do I delete a file named “-p” from bash?

How do I delete a file named -p in bash? Trying rm "-p" complains that -p is not a valid argument.
7
votes
7answers
62k views

How can I run a shell script in Windows?

I'm a Mac user and new to Windows. How can I execute a shell script in Windows? Generally, I would save the text file with the extension .sh and run it via Terminal. But how could I do that in ...
7
votes
4answers
888 views

Looking for a regular expression to work on a list of comma separated values

I have line of 3 long numbers separated by comma. What I want to do is to delete the number in the middle, and leave only first number and last number separated by a comma. Currently: ...
7
votes
2answers
1k views

Writing shell scripts that will run on any shell (using multiple shebang lines?)

I've just started getting deeper into shell scripting, and I've always just thrown my script in a file, marked it chmod +x and then done /path/to/script.sh and let whatever interpreter is the default ...
7
votes
1answer
1k views

Shell script to find all types of files in a directory with their count

I want to write a shell script or alias to find the number of different types of files in a directory. For example, sample output of script should be: *.h 20 *.c 40 *.cpp 10 Makefile 3
7
votes
2answers
2k views

Gnu Screen: Execute command on attach

Is there a way to have GNU Screen detect when it is being attached and to execute a shell script that I have whenever it is attached? The reason for this is that I want to keep my display variables ...
7
votes
1answer
317 views

How to change the appearance of empty folders in finder using Automator?

I'm trying to use Apple Automator to label empty folders in a heirarchy. Basically, I start off a project with a heirarchy of empty folders and fill them as I work. I want a workflow to label all ...
6
votes
2answers
24k views

How to find all soft links (symbolic links) in current directory?

Question relates to shell-scripting in bash. How to check with a script which files within the current directory are soft links? In case I have used the wrong term, when I say soft links, I am ...
6
votes
5answers
5k views

How to echo commands in a bash shell script, but not execute them?

Is there a way for running a shell script with echoing the commands but w/o actually executing them? Let's say I have script removing a file whose name is stored in a variable: #!/bin/bash set -v ...
6
votes
1answer
21k views

Bash & 'su' script giving an error “standard in must be a tty”

Folks, I'm having an issue with a bash script which runs a particular command as a different user. The background: Running on a Linux box (CentOS), the script is quite simple, it's starting the ...
6
votes
2answers
3k views

Recursive unrar of several folders

I have several RAR archives spread around multiple directories but all under a particular root folder on my Debian based NAS. Could someone help me write a simple script that would recursively go into ...
6
votes
3answers
438 views

Portable method of opening a new terminal window?

So, I'm working on a custom makefile script (for TinyOS, if anyone cares) that builds a file and then runs it in a simulator. The problem is that the simulator takes it's input from standard in, and ...
6
votes
4answers
931 views

Run Linux command as predefined user

I have created a shell script to start a server program. startup.sh start When above command will executes, it will try starts the server as adminuser. To achieve this my script has written like ...
6
votes
5answers
9k views

Run a shell script on OS X without having a terminal window appear?

I'm trying to set up a few keyboard shortcuts that open specific iTerm sessions, which I was able to do with BetterTouchTool and a bit of AppleScript magic. The problem is that OS X insists to open a ...
6
votes
2answers
66 views

Process files in a folder that haven't previously been processed

I have a series of files in a directory that I need to carry an action out on using a script. Once the action is done, then I want to keep a log that the file has been processed, so that the next ...
5
votes
6answers
357 views

Find all filename.ext1 where no filename.ext2 exists in OS X or the shell

I need to create both TIF and JPG versions of a large set of images. All JPG images already exist, but only a part of the TIF images. Is there an easy way to search a directory to find all JPG files ...
5
votes
6answers
10k views

Starting a linux process in the background

I'm trying to create a script which starts some programs startup.sh knetworkmanager emesene keepassx The problem is that when I run the script, it only starts knetworkmanager. This is because ...
5
votes
2answers
3k views

How to extract a complete list of extension types within a directory?

Within a directory, and recursively within it's sub-directories, meaning every directory within a directory is processed, how do I compile a complete list of unique extensions within the directory? ...
5
votes
3answers
4k views

Test if a package is installed in APT

I want a shell script method to test/report if a package is installed. I don't need details, only boolean return to set logic flow. I looked at Find if a package is installed, but dpkg returns complex ...
5
votes
3answers
2k views

How to script a database migration using an SSH tunnel?

I am trying to create a sync.sh script that coordinates with our less-than-ideal version control for our website. This script will migrate a live copy of the database to our development environment ...
5
votes
2answers
288 views

Bash: How to list n random number of files (not head or tail)

I have a large directory containing numerous files of similar kind. I have to send few random files for auditing. These files should not be from the top or bottom (eg. not in head or tail). This is ...
5
votes
1answer
5k views

Why do I Get [[: not found When Running a Script?

I'm trying to write a script that has to check if a file exists. In the console I write if [[ -a /path/to/file.txt ]]; then echo "not mod"; else echo "mod"; fi and I get not mod but when I write ...
5
votes
1answer
2k views

Echo text in a certain color in a shell script

I am about to write a shell script. I would like to be able to visually distinguish some of its output text as a warning, by setting its color to yellow. How is this done?
5
votes
2answers
258 views

How to delete group of folders based on name and date

I have a folder structure like this. folders test1 test2.1 test49.85 test4.95.89 sample support util These are all folders. I need to delete all folders that begin with ...
5
votes
2answers
288 views

Progress Indicator-Falling deck of cards(Bash)

I coded a progress indicator in the form of a falling deck of cards (my wife's idea). It's not in its final form yet. Here is the code: vdeck="| | | | | " hdeck[1]="__ | | | |" hdeck[2]="__ __ | | ...
5
votes
1answer
2k views

Reading values from plist nested dictionaries in shell script

I have a plist file and I'm interested in fetching values from nested dictionaries using shell script. Here is the structure of my plist file: <dict> <key>Countries</key> ...
4
votes
2answers
268 views

Use of '#!' in *nix scripts

In many Apple/Unix scripts I see stuff preceeded by '#" which as far as I know means it is a remark. However often you see shell calls as below (the line with #!). ## Setting a default route ...
4
votes
2answers
134 views

How can I take the output of a shell script and place it in a file on the command line?

How can I take the output of a shell script and place it in a file on the command line?
4
votes
4answers
2k views

with a shell script, it is possible to tell when it's run with cron vs run manually?

with a shell script, it is possible to tell when it's run with cron vs run manually? UPDATE: it was asked why I want to know. My cron job will be logging, and i want to be able to log if someone ...
4
votes
3answers
3k views

How to replace part of a text file between markers with another text file?

Say I have a text file like this: # custom content section a b ### BEGIN GENERATED CONTENT c d ### END GENERATED CONTENT I'd like to replace the portion between the GENERATED CONTENT tags with the ...
4
votes
1answer
2k views

How to find all zero byte files in directory

How to find all zero bytes files in directory and even in subdirectories i done this #!/bin/bash lns=`vdir -R *.* $dir| awk '{print $8"\t"$5}'` temp="" for file in $lns do if test $file = "0" then ...