bash

Is it worth learning BASH when I know Perl?

All my scripting is done in Perl, I can execute one liners at the command line, and Perl regex seems way easier. Is there anything I can do in BASH that I can't do in Perl? I just don't feel like a true hacker unless I spend the time to delve in BASH and start using Sed and Awk as well. Is it worth it, or am I just asking for pain and...

Find all storage devices attached to a Linux machine

I have a need to find all of the writable storage devices attached to a given machine, whether or not they are mounted. The dopey way to do this would be to try every entry in /dev that corresponds to a writable devices (hd* and sd*). Is there a better solution, or should I stick with this one? ...

How do I reuse a command in bash with different parameters?

I have two scripts that often need to be run with the same parameter: $ populate.ksh 9241 && check.ksh 9241 When I need to change the parameter (9241 in this example), I can go back and edit the line in history. But since I need to change the number in two places, I sometimes make a typo. I'd like to be able to change the parameter ...

What is the Best Way to Perform Timestamp Comparison in Bash

I have an alert script that I am trying to keep from spamming me so I'd like to place a condition that if an alert has been sent within, say the last hour, to not send another alert. Now I have a cron job that checks the condition every minute because I need to be alerted quickly when the condition is met but I don't need to get the ema...

Fast recursive grepping of svn working copy

I need to search all cpp/h files in svn working copy for "foo", excluding svn's special folders completely. What is the exact command for GNU grep? ...

How to rename with prefix/suffix ?

How do I do mv original.filename new.original.filename without retyping the original filename? I would imagine being able to do something like mv -p=new. original.filename or perhaps mv original.filename new.~ or whatever - but I can't see anything like this after looking at man mv / info mv pages. Of course, I could write a shell scri...

Can I use Python as a bash replacement?

I currently do my textfile manipulation through a bunch of badly remembered awk, sed, bash and a tiny bit of Perl. I've seen mentioned a few places that python is good for this kind of thing, I know a little and I would like to know more. Is python a good choice for this, and is there a good book or guide to learning how to use python t...

Hidden features of Bash

Shell scripts are often used as glue, for automation and simple one-off tasks. What are some of your favorite "hidden" features of the Bash shell/scripting language? One feature per answer Give an example and short description of the feature, not just a link to documentation Label the feature using bold title as the first line See al...

Convert text to 7-bit ASCII from command-line

I'm on OS X 10.5.5 (though it does not matter much I guess) I have a set of text files with fancy characters like double backquotes, ellipsises ("...") in one character etc. I need to convert these files to good old plain 7-bit ASCII, preferably without losing character meaning (that is, convert those ellipses to three periods, backqu...

How to read mutliline input from stdin into variable and how to print one out in shell(sh,bash)?

What I want to do is the following: read in multiple line input from stdin into variable A make various operations on A pipe A without loosing delimiter symbols (\n,\r,\t,etc) to another command The current problem is that, I can't read it in with read command, because it stops reading at newline. I can read stdin with cat, like thi...

How do I print a field from a pipe-separated file?

I have a file with fields separated by pipe characters and I want to print only the second field. This attempt fails: $ cat file | awk -F| '{print $2}' awk: syntax error near line 1 awk: bailing out near line 1 bash: {print $2}: command not found Is there a way to do this? ...

Why does an SSH remote command get fewer environment variables then when run manually?

I have a command that runs fine if I ssh to a machine and run it, but fails when I try to run it using a remote ssh command like : ssh user@IP <command> Comparing the output of "env" using both methods resutls in different environment variables. When I manually login to the machine and run env, I get much more environment variables t...

How can I use negative wildcards in a unix/linux shell?

Say I want to copy the contents of a directory excluding files and folders whose names contain the word 'Music'. cp [exclude-matches] *Music* /target_directory What should go in place of [exclude-matches] to accomplish this? ...

bash regex with quotes?

The following code number=1 if [[ $number =~ [0-9] ]] then echo matched fi works. If I try to use quotes in the regex, however, it stops: number=1 if [[ $number =~ "[0-9]" ]] then echo matched fi I tried "\[0-9\]", too. What am I missing? Funnily enough, bash advanced scripting guide suggests this should work. Bash version 3....

What linux shell command returns a part of a string?

I want to find a linux command that can return a part of the string. In most programming languages, it's the substr() function. Does bash have any command that can be used for this purpose. I want to be able to do something like this... substr "abcdefg" 2 3 - prints 'cde' Subsequent similar question: http://stackoverflow.com/questio...

what's a more concise way of finding text in a set of files?

I currently use the following command, but it's a little unwieldy to type. What's a shorter alternative? find . -name '*.txt' -exec grep 'sometext' '{}' \; -print Here are my requirements: limit to a file extension (I use SVN and don't want to be searching through all those .svn directories) can default to the current directory, bu...

How do I prompt for input in a Linux shell script?

I want to pause input in a shell script, and prompt the user for choices. The standard 'Yes, No, or Cancel' type question. How do I accomplish this at a typical bash prompt? ...

How do I make Bash history permanent under FreeBSD?

Hello, I have a FreeBSD box with Bash set up. I'm used to Debian Linux where my command history is automatically saved when I log out, so that when I log back in I can press up or Ctrl-R or whatever and have access to my history. How do I set up Bash to do this on FreeBSD? ...

How to tell if a string is not defined in a bash shell script?

If I want to check for the null string I would do [ -z $mystr ] but what if I want to check whether the variable has been defined at all? Or is there no distinction in bash scripting? ...

String contains in bash

Using bash, I have a string: string=`echo My string` How can I test if it contains another string? if [ $string ?? 'foo' ] then; echo "It's there!"; fi; Where ?? is my unknown operator. Do I use echo and grep? if [ `echo $string || grep 'foo' ` ] then; echo "It's there!"; fi; That looks a bit clumsy. ...