bash

getopts in bash programming

when i am using 'while getopts d:n: OPTION' - how do i know that i got only invalid option and if yes that there is an argument after it? ...

pattern matching in Bash

Hi, Here is an example to get different parts of a filename bash-3.2$ pathandfile=/tmp/ff.txt bash-3.2$ filename=$(basename $pathandfile) bash-3.2$ echo $filename ff.txt bash-3.2$ echo ${filename##*.} txt bash-3.2$ echo ${filename%.*} ff I was wondering what does ## and % mean in the patterns. How is the ...

Piping a bash variable into awk and storing the output

Hello, To illustrate my problem, TEST="Hi my name is John" OUTP=`echo $TEST | awk '{print $3}'` echo $OUTP What I would expect this to do is pass the $TEST variable into awk and store the 3rd word into $OUTP. Instead I get "Hi: not found", as if it is expecting the input to be a file. If I pass just a string instead of a variable,...

Find all writable files in the current directory

I want to quickly identify all writable files in the directory. What is the quick way to do it? ...

ssh script gives "key_read" error

I'm using a script that connects to a cluster through ssh and sends some commands, then quits the connection. This script basically connects once using ssh, then executes a script in this session. This script loops through a list of commands a few times and after it is finished, the connection is terminated. So this script works fine, ...

Use regex in awk command in bash script

I'm trying to read a file of regexes, looping over them and filtering them out of another file. I'm so close, but I'm having issues with my $regex var substitution I believe. while read regex do awk -vRS= '!/$regex/' ORS="\n\n" $tempOne > $tempTwo mv $tempTwo $tempOne done < $filterFile $tempOne and $tempTwo are temporary files. ...

How do a script know his own PID ??

I have a script in bash called Script.sh, and i need to know his own PID ( i need to get PID inside the Script.sh ) Any clues to realize this ? Regards, Debugger ...

how to write a bash script like the ones used in init.d?

I have to write a bash script that makes lot of things. I'd like to print messages as nice as init scripts do. For example: Doing A... [OK] Doing B... [ERROR] .... Do you know any way to make this? Thanks in advance ...

How do I delete folders in bash after successful copy (Mac OSX)?

Hello! I recently created my first bash script, and I am having problems perfecting it's operation. I am trying to copy certain folders from one local drive, to a network drive. I am having the problem of deleting folders once they are copied over, sometimes the folder would be empty of contents, but the folder itself would remain, other...

Safe way to set computed environment variables

I have a bash script that I am modifying to accept key=value pairs from stdin. (It is spawned by xinetd.) How can I safely convert those key=value pairs into environment variables for subprocesses? I plan to only allow keys that begin with a predefined prefix "CMK_", to avoid IFS or any other "dangerous" variable getting set. But the si...

extract domain name from url

How do I extract the domain name from a url using bash? like: http://example.com/ to example.com must work for any tld, not just .com ...

running a bash script from a make file

I have a makefile from which I want to call another external bash script to do another part of the building. How would I best go about doing this. ...

Exclude a string from wildcard search in a shell

Hello everybody I am trying to exclude a certain string from a file search. Suppose I have a list of files: file_Michael.txt, file_Thomas.txt, file_Anne.txt. I want to be able and write something like ls *<and not Thomas>.txt to give me file_Michael.txt and file_Anne.txt, but not file_Thomas.txt. The reverse is easy: ls *Thomas....

Bash or python for changing spacing in files

Hi, I have a set of 10000 files. In all of them, the second line, looks like: AAA 3.429 3.84 so there is just one space (requirement) between AAA and the two other columns. The rest of lines on each file are completely different and correspond to 10 columns of numbers. Randomly, in around 20% of the files, and due to some errors, on...

how does ` cat << EOF` work in bash?

I needed to write a script to enter multi-line input to a program (psql) After a bit of googling, I found the following syntax works: cat << EOF | psql ---params BEGIN; `pg_dump ----something` update table .... statement ...; END; EOF This correctly constructs the multi-line string (from BEGIN; to END;, inclusive) and pipes it as ...

Can't get expand_aliases to take effect

I can't get expand_aliases to take effect in bash. I've tried a lot of different things, and nothing works. Here's the simple test case: /bin/bash -c 'shopt -s expand_aliases; alias cdtmp="cd /tmp"; alias; cdtmp; pwd;' And the output: $ /bin/bash -c 'shopt -s expand_aliases; alias cdtmp="cd /tmp"; alias; cdtmp; pwd;' alias cdtmp='c...

bash script to delete old deployments

I have a directory where our deployments go. A deployment (which is itself a directory) is named in the format: <application-name>_<date> e.g. trader-gui_20091102 There are multiple applications deployed to this same parent directory, so the contents of the parent directory might look something like this: trader-gui_20091106 trader...

bash - how to filter java exception info

Hi all: We've got a multi-agent Java environment where different agent would most likely produce all sorts of exceptions thrown to stderr. Here is a sample taken from the huge exception log **java.security.AccessControlException: access denied (java.io.FilePermission ..\tournament\Driver\HotelRoomAnalyser.class read)** at java.sec...

Backup of folder + database - Python

Hi there, I feel like this is quite delicate, I have various folders whith projects I would like to backup into a zip/tar file, but would like to avoid backing up files such as pyc files and temporary files. I also have a Postgres db I need to backup. Any tips for running this operation as a python script? Also, would there be an...

uppercase to lowercase in bash on a mac

Hi I'm writing a bash script which needs to convert a string to lowercase. The problem is I'm doing it on a mac so 'tr' is not available. How can I go about doing this on a mac? The problem I'm trying to tackle is that my script needs to recognize an if an extension is a .gif or a .jpg - and I don't want to have to check for .jpeg, .jP...