Bash scripting is making scripts in the Bash shell language.

learn more… | top users | synonyms

10
votes
2answers
10k views

How do I enter a literal tab character in a bash shell?

For example, I wanted to use the sort utility with the -t option to specify tab separators, but sort -t "\t" doesn't work.
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
4answers
5k views

how to grep and print the next N lines after the hit?

I would like to grep for an occurrence in a text file, then print the following N lines after each occurrence found. Any ideas?
8
votes
1answer
371 views

How to make sed omit not matching lines?

In the following example, sed matches lines starting with an a or a c and prints the first character of that line (a or c): $ echo "ag bh ci dj ek fl" | sed 's/\(a\|c\)./\1/' # Matches lines starting ...
8
votes
1answer
586 views

why doesn't a bash while loop exit when piping to terminated subcommand?

Why doesn't the command below exit? Rather than exit, the loop runs indefinitely. While I discovered this behavior using a more complex setup, the simplest form of the command reduces to the ...
8
votes
4answers
5k views

How to truncate file by lines?

I have a large number of file, some of which are very long. I would like to truncate them to a certain size if they are larger by removing the end of the file. But I only want to remove whole lines. ...
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
2answers
1k views

bash completion for filename patterns or directories

I'm trying to get a bash completion script set up and having some trouble. I would like to set it up so the completions listed are either files matching a particular extension, or directories (which ...
6
votes
1answer
2k views

Automated Interaction with Google Chrome

All right SU, I have a fun nut to crack. I have 8 screens set up in a line, all hooked into one beefy G5 running Snow Leopard (yes, 4 graphics cards; about 15,000 pixels wide!). I am setting up a ...
6
votes
5answers
2k views

Delete all files (except the four newest) throughout directories using a Bash script

I'm looking for a Bash script that will go into a list of directories and delete all but the four most recently created files. How can I do this?
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
5answers
948 views

How to move all sibling folders into one folder?

I apologize that this seems like such a dumb question, but I can't afford to screw it up. I have a bunch of folders like so: /users/me/foo/oops1 /users/me/foo/oops2 ...
4
votes
1answer
369 views

BASH function not escaping control characters

Hey guys I have a function that I'm using to find stuff, but unfortunately anytime I pass it a control character ($intVal or testing : etc) it chokes. I was wondering what the fix was? I can ...
4
votes
1answer
53 views

How to filter output from a command in OS X

I am in need to monitor the the writes from an unknown application to a known directory through creatbyproc.d. I'm going to have to leave this up and running for up to 36 hours which will not be ...
3
votes
3answers
5k views

Process each line of a file in bash

When I want to do something with each line in a file I usually write cat my_file | while read a do gzip "$a" done The gzip is just an example it can be anything. What I'm wondering is if there is ...
3
votes
4answers
4k views

awk / sed to print only up to the underscore character

How can I use awk or sed to print a string only up to the first underscore character? Before: host100_044 2 host101_045 2 host102_046 2 After: host100 host101 host102
3
votes
2answers
116 views

Append dir to PATH

I'm trying to localy add a dir to the global $PATH variable. I added the following to my .bashrc. export PATH=$PATH:$VRS/bin But PATH seems to get concatenated to itself over & over every time ...
3
votes
5answers
712 views

Convert string “1-5” into array of numbers in bash

What is the best approach to convert the string containing the range of numbers into numbers itself? This format is used for example in defining the range of pages to print. I define, I want to print ...
3
votes
5answers
620 views

What is the simplest way to remove lines from one file matched with lines from another file?

What's the simplest way to remove lines from one file matched with lines from another file? For example, if I have the following files: file1.csv: u2@domain.com file2.csv: ...
3
votes
2answers
178 views

Is a subshell executed with a here string?

I'm trying to better organise a bash script as part of a re-write/re-organisation and am wondering what "best practice" is in the following situation: ymd=`echo "${NOEXT}" | egrep -o -m 1 ...
3
votes
2answers
116 views

Found a script on my cron.monthly, i am wondering what's it doing?

I am new to bash-programming, sorry if this might be a kind of stupid question. I found a script on my server and i am trying to understand every line: #!/bin/sh # # man-db cron weekly set -e if ! ...
3
votes
1answer
18 views

Bash: optionally passing arguments to a command

I'm trying to add arguments to a command call depend on another variable. Please look at the shell scripting code: curl \ $([ -z "${title}" ] || echo --data-urlencode title=${title}) \ ...
3
votes
1answer
475 views

Parse multiple HTML to text and rename as parent Directory

In a directory there are many folders .html inside, I would like all HTML parsed as new .txt with the name of the parent directory. Example1/ Index.html>Example1.txt Example2/ ...
3
votes
3answers
107 views

Why doesn't this script work?

I've been using this bash script: for i in $(ls); do mv $i a$i; done to prepend all filenames in a folder with the letter a. I'm afraid that at some point I'll accidentally use this script in the ...
3
votes
2answers
36 views

How to work around Bash for loop not exiting when a subcommand errors?

I have a for-loop in a shell script a la: #!/bin/bash set -u set -e for l in sh rb py php java cs; do (cd $l; ./run-tests.sh) done The intent is to have the for-loop die in a fire when any one of ...
3
votes
1answer
428 views

Script-specific autocomplete in shell

I create a lot of little scripts to help me in my every day life. I would like to provide auto-complete for them, specially that I intend to share them with people. Now, I know I can create ...
3
votes
3answers
68 views

merge pieces of file together

I have a directory tree of files I need to loop through and reformat the lines. can I do this with sed with some combination of find? for each file, I'll need to adjust the text in the file from ...
3
votes
1answer
60 views

Checking pids under all users and re-launching the processes after reboot

Is there a way to check all pids of all users under /home directory, and to run these processes again after a reboot or shutdown? For example, if user1 was running a psybnc and user2 was running an ...
3
votes
1answer
595 views

How to get list of all child process spawned by a script

Context: Users provide me their custom scripts to run. These scripts can be of any sort like scripts to start multiple GUI programs, backend services. I have no control over how the scripts are ...
2
votes
2answers
913 views

how to properly use $1 in an alias with two arguments

i have to compare a number of files and I don't want to change the command in two places all the time. so i want to create an alias in bash. alias gd='gvimdiff $1 dir/$1' so that i can get ...
2
votes
1answer
249 views

Bash script: “[[: not found”

This is my code: #!/bin/bash if [[ -d ~/viwiki ]]; then cd ~/viwiki else mkdir ~/viwiki cd ~/viwiki fi if ! [[ -d ./log ]]; then mkdir log mkdir log/log mkdir "log/wget" elif ! [[ -d ./log/log ...
2
votes
1answer
4k views

Howto switch / chage user id witin a bash script to execute commands in the same script?

Is there a way to switch user identity within a script (executed as root as part of an installation process) to execute some commands without calling an external script, then return to root to run ...
2
votes
5answers
690 views

Truncate file in a pipe

Is there a simple way to truncate a file in a pipe? Specifically, I want to chop of the last four bytes of a file before feeding it into another process. Ideally, I'd be able to write something like: ...
2
votes
1answer
4k views

Handle filename with spaces inside Bash-script

In my Bash-script i have to handle filenames with spaces. These are the important lines inside my script: mp3file="/media/d/Music/zz_Hardcore/Sampler/Punk-O-Rama\ Vol.5\ \[MP3PRO\]/01\ -\ Nofx\ -\ ...
2
votes
1answer
117 views

How to get the result from bash auto-completion as a string?

What I'm looking for is not how to add autcompletion in bash for another program, but a way to get the result of what would have happened in bash to a string, basically: autocomplete "/etc/apa" ...
2
votes
1answer
6k views

How can I compare a variable to a text string, rather than integer, in an if/else statement?

In the process of writing a shell script, I ran into an issue with the following if/else statement that falls somewhere in the middle of the script: if [ $act -eq "add" ] then read - "add or ...
2
votes
1answer
87 views

Invoke a script for every command typed at shell prompt?

On bash, is there any simple way to run a command every time another command is entered in your shell? I know about 'trap ... DEBUG', but unfortunately it does not run the specified code for every ...
2
votes
2answers
468 views

Putting the cursor back at the bash prompt after a script returns delayed output

Just a minor issue. I'm writing a simple bash script that starts and stops Jetty. When I execute it, the script immediately puts my cursor back on the bash prompt. However, as Jetty starts up and ...
2
votes
2answers
1k views

How do I use the -ne flag for echo in a shell script?

I'm trying to create a shell script to take an argument and use it to name a terminal tab. So if the script's name is tabnm, tabnm "test" should rename the current tab "test" This is my code: ...
2
votes
2answers
463 views

appending timestamp to tail -f results

I would like to have a timestamp printed in front of every line that comes out of a "tail -f outputfile", so that I know when is each line printed in. Something like: [...] 20110617_070222:results ...
2
votes
2answers
468 views

Prefix to each output of a command on runtime

I am trying to make a modular script. I have several scripts/commands which are called from a single script. I want to prefix the output of each separate command. Examle: My files are allcommands.sh ...
2
votes
1answer
38 views

Is this a bash-specific method of calling a script: VARIABLE=value bash somescript

I have worked all my life in HPUX/ksh shell, so bash these days seems to be throwing new ways of doing many things (many of which don't seem to be POSIX compliant - for example 'mkdir -p ...
2
votes
1answer
32 views

Are shebang invocations detectable?

Is there a way for a bash script to detect if it's being run via the #!/usr/bin/env shebang? Some magic bash foo that would let me write: #!/bin/bash ... if [ __INVOKED_VIA_USR_BIN_ENV__ ]; then ...
2
votes
2answers
99 views

Delete files and exclude some with find [duplicate]

It was an old problem. I knew how to delete files and exclude some, like this: rm `find ~/temporary/Test\ 1 -mindepth 1 -maxdepth 1|grep -v 'A'` but the problem is the folder 'Test 1' containing a ...
2
votes
1answer
242 views

linux find: searching for files smaller than 2Kb misses the ones between 1 and 2Kb

I just stumbled in an oddity with the find command on Linux, in a nutshell I had the need to find all files smaller than 2Kb in a certain folder, so I issued the command: find /folder_to_search_into/ ...
2
votes
1answer
71 views

How to properly restore settings when running a script using “source”?

I'm writing a script that needs to be executed using source, because its task is to modify the environment. During the script, I want to use set -e so that any error stops the script. The script looks ...
2
votes
1answer
390 views

How to calculate and group the time differences of specified syscalls when running strace

I'm planing to do some web performance testing (to see how much time it's spent for each file), but it could be useful for any application. I exactly know existence and how to use different ...
2
votes
2answers
426 views

Word splitting does not see my quotes

Imagine I have this $ ARGS='"a b" c' $ for arg in "$ARGS"; do echo "$arg"; done "a b" c $ for arg in $ARGS; do echo "$arg"; done "a b" c The result I'm looking to get is $ <???> a b c How ...
2
votes
4answers
2k views

BASH - grep - works on command line, but not script

I am trying to get the number of occurrences in a script and when running the command on the command line, it works fine, but not in a script. Both variables are initialized. FILE_PATH is the ...
2
votes
1answer
27 views

Can you delete all users but specified accounts in Ubuntu?

I am trying to find a way to quickly delete all user accounts except for the ones I specify in Ubuntu. Is there a good way of doing this? Perhaps a bash script that could do this function? I ...