bash

Using the star sign in grep

I am trying to search for the substring "abc" in a specific file in linux/bash So I do: grep '*abc*' myFile It returns nothing. But if I do: grep 'abc' myFile It returns matches correctly. Now, this is not a problem for me. But what if I want to grep for a more complex string, say *abc * def * How would I accomplish it using ...

go to path and then tar?

Can you have tar travel to a certain direct and then tar files relative to that directory? All while using one command (tar)? For example instead of doing cd /home/test/backups; tar zvPcf backup.tar.gz ../data/ I could do something like tar -g '/home/test/backups/' zvPcf backup.tar.gz ../data/ ...

Execute a shell command from a shell script without stopping if error occurs

In a sort of try/catch form I want to execute a bash that doesn't stop if an error occurs. The specific bash is: #!/bin/sh invoke-rc.d tomcat stop rm -fr /var/webapps/ cp -R $WEBAPP /var/webapps/ invoke-rc.d tomcat start I want to exec "invoke-rc.d tomcat stop" and even if Tomcat is not running, continue to execute the other bash c...

Shell Scripting - pipes and redirection

I'm using CocoaDialog to present some feedback during execution of a download script. I wish to present an indeterminate progress bar whilst command operation us taking place. This is possible by piping text to CocoaDialog for the duration of the operation. http://cocoadialog.sourceforge.net/documentation.html#progressbar_control I tho...

ShellScript: Whats the diff between % and %% here?

Hi, I am a shell script newbie. I want to know the difference between ${var%pattern} and ${var%%pattern} Thanks ...

How to deal with NFS latency in shell scripts

I'm writing shell scripts where quite regularly some stuff is written to a file, after which an application is executed that reads that file. I find that through our company the network latency differs vastly, so a simple sleep 2 for example will not be robust enough. I tried to write a (configurable) timeout loop like this: waitLoop...

grep vs Perl. Should I mod this to work, or should I just replace it with Perl code?

I am writing something that will allow users to search through a log of theirs. Currently I have this, where $in{'SEARCH'} is the string they are searching. open(COMMAND, "grep \"$in{'SEARCH'}\" /home/$palace/palace/logs/$logfile | tail -n $NumLines |"); $f = <COMMAND>; if ($f) { print $Title; print "<div id=log>\n"; ...

Reading a subset of the lines in a text file, with bash

Hi! I have a file line a - this is line a line b - this is line b line c - this is line c line d - this is line d line e - this is line e The question is: How can I output the lines starting from "line b" till "line d" using bash commands? I mean, to obtain: "line b - this is line b line c - this is line c line d - this is line d" ...

How to get number of rows affected, while executing mysql query from bash?

Now I know how one can execute mysql queries \ commands from bash : mysql -u[user] -p[pass] -e "[mysql commands]" or mysql -u[user] -p[pass] `<<`QUERY_INPUT [mysql commands] QUERY_INPUT My question is : How can I capture how many rows where affected by the query? I tried doing: variable='`mysql -u[user] -p[pass] -e "[mysql comm...

Write a shell script that find-greps and outputs filename and content in 1 line

To see all the php files that contain "abc" I can use this simple script: find . -name "*php" -exec grep -l abc {} \; I can omit the -l and i get extracted some part of the content instead of the filenames as results: find . -name "*php" -exec grep abc {} \; What I would like now is a version that does both at the same time, but on...

How do you automatically colorize program outputs in a bash shell?

I want to take any program that outputs to the screen, catch the output, and colorize certain keywords before they are output to the screen. For example, here's the normal program output: bash# <program> blah blah blah <-- this output has no color vs. bash# <program> blah blah blah <-- this output is colorful Ideally it...

Can 'find' or any other tool search for files breadth-first?

Sometimes I know a file is not so deep away, but a very dense sub-directory does not allow me to find the files I want easily. Can find (or any other tool) look for files using breadth-first search? ...

bash, dash and string comparison

While writing a fairly simple shell script, I've tried to compare two strings. I was using /bin/sh instead of /bin/bash, and after countless hours of debugging, it turns out dash (which is actually dash) can't handle this block of code: if [ "$var" == "string" ] then do something fi What is a portable way to compare strings uding...

counting duplicates in a sorted sequence using command line tools

I have a command (cmd1) that greps through a log file to filter out a set of numbers. the numbers are in random order, so i use sort -gr to get a reverse sorted list of numbers. there may be duplicates within this sorted list. I need to find the count for each unique number in that list. For e.g. if the output of cmd1 is 100 100 ...

get current time in seconds since the Epoch on Linux, Bash

I need something simple like date, but in seconds since 1970 instead of the current date, hours, minutes, and seconds. Date doesn't seem to offer that option. Is there an easy way? ...

Inserting a line to a known block of text

I define a 'block' of text as all lines between start of file, newline or end of file: block1 block2 block3 anotherblock4 anotherblock5 anotherblock6 lastblock7 lastblock8 Any text can occupy a block - it is unknown what lines are there. I tried to write a shell script to insert a new line at the 2nd block, but since sed doesn't lik...

Run bash script from Windows PowerShell

In cygwin, I could just do ./script.sh args, but this opens the script file in notepad in PowerShell. What do I need to do have it execute? ...

How can I traverse a directory tree using a bash or Perl script?

I am interested into getting into bash scripting and would like to know how you can traverse a unix directory and log the path to the file you are currently looking at if it matches a regex criteria. It would go like this: Traverse a large unix directory path file/folder structure. If the current file's contents contained a string tha...

BAsh Script -- logname validation

How do I validate that the LOGNAME is present in a bash script if [`logname`]; then echo -e \\t "-- Logname : `logname`" >> $normal_output_filename fi The above gives me an error line 76: [logname]: command not found ...

Humanized dates with awk?

I have this awk script that runs through a file and counts every occurrence of a given date. The date format in the original file is the standard date format, like this: Thu Mar 5 16:46:15 EST 2009 I use awk to throw away the weekday, time, and timezone, and then do my counting by pumping the dates into an associative array with the date...