Bash scripting is making scripts in the Bash shell language.

learn more… | top users | synonyms

10
votes
2answers
9k 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
7k 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
1answer
317 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
536 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
3answers
4k views

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
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?
7
votes
1answer
890 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
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 ...
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
2answers
65 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
891 views

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 ...
5
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?
4
votes
1answer
343 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
50 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
4k 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
3k 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
115 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
673 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
552 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
169 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
113 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
424 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
1answer
408 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
59 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
517 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
819 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
228 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
5answers
657 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
103 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
5k 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
85 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
455 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
430 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
381 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
31 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
85 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
64 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
2answers
385 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
23 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 ...
2
votes
2answers
88 views

How can sed find pattern in one field but append another(same row)

I have a text file that has comma separated values. Field1 has field that needs to be matched, but field two needs substitution based on the match. Example: A,C B,C Result: A,D B,C I'm guessing ...
2
votes
1answer
211 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
3answers
199 views

Shell script not running as by “./” but it runs by “sh”

I am running a script on arm target board as a super user, i.e., as a root user. But the script is not executing if i run it with following syntax root@freescale$ ./Script.sh -sh: ./Script.sh: ...
2
votes
2answers
330 views

concatenate files including path in header - path contains spaces

I have to concatenate a number of files in a directory structure which contains spaces in the folder names looking like this: ./CH 0000100014/A10/11XT/11xt#001.csv find . -name "*.csv" -type f ...
2
votes
1answer
417 views

A bash script to start applications in certain workspaces [duplicate]

I'm dreaming of a Bash script that opens applications and places them in certain workspaces. For example, I'd like to run a script that: In Workspace 1 opens firefox WS2 - opens term and maximizes ...
1
vote
3answers
1k views

How could I make a bash script to execute apt-get?

I'm trying to automatize some configurations I have with bash script, I've never done this before so I tried with something easy like a Hello World! and everything works just fine, but then I tried ...