shell-scripting

Elegant way to search for UTF-8 files with BOM?

For debugging purposes, I need to recursively search a directory for all files which start with a UTF-8 byte order mark (BOM). My current solution is a simple shell script: find -type f | while read file do if [ "`head -c 3 -- "$file"`" == $'\xef\xbb\xbf' ] then echo "found BOM in: $file" fi done Or, if you prefer s...

JavaDoc like framework for shell scripts?

Is there an open source or public domain framework that can document shell scripts similar to what JavaDoc produces? I don't need to limit this just to a specific flavor of shell script, ideally I would like a generic framework for documenting API or command line type commands on a web page that is easy to extend or even better is self d...

Asynchronous shell exec in PHP

I've got a PHP script that needs to invoke a shell script but doesn't care at all about the output. The shell script makes a number of SOAP calls and is slow to complete, so I don't want to slow down the PHP request while it waits for a reply. In fact, the PHP request should be able to exit without terminating the shell process. I've lo...

How do I calculate the previous business day in ksh shell script ?

What is the most elegant way to calculate the previous business day in shell ksh script ? What I got until now is : #!/bin/ksh set -x DAY_DIFF=1 case `date '+%a'` in "Sun") DAY_DIFF=2 ;; "Mon") DAY_DIFF=3 ;; esac PREV_DT=`perl -e '($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time()-${DAY_DIFF}*24*60*60...

Provide password using Shell script

I had setup my clients & server for passwordless login. Like passwordless login by copying RSA key of server to all client's /root/.ssh/id-rsa.pub. but this, I have done manually. I like to automate this process using shell script and providing password to the machines through script. If this problem is solved then I also want to use rsy...

Setting environment variables in linux using bash

In tcsh, I have the following simple script working: #!/bin/tcsh setenv X_ROOT /some/specified/path setenv XDB ${X_ROOT}/db setenv PATH ${X_ROOT}/bin:${PATH} xrun -d xdb1 -i $1 > $2 My question is, what is the equivalent to the tcsh setenv function in bash? Is there a direct analog? The environment variables are for locating ...

unix shell script find out which directory the script file resides?

Basically I need to run the script with paths related to the shell script file location, how can I change the current directory to the same directory as where the script file resides? ...

Shell script to find bigrams.

I'm making a shell script to find bigrams, which works, sort of. #tokenise words tr -sc 'a-zA-z0-9.' '\012' < $1 > out1 #create 2nd list offset by 1 word tail -n+2 out1 > out2 #paste list together paste out1 out2 #clean up rm out1 out2 The only problem is that it pairs words from the end and start of the previous sentence. eg for th...

Is is possible to do mget with file patterns to only download certain files?

I know we can do like mget *.xml which will download all xml files. But how is it possible that we using mget with certain file name patterns. can we do something like *SS.xml which mean it will download all files ending with SS.xml? ...

Why doesn't "cd" work in a bash shell script?

I'm trying to write a small script to change the current directory to my project directory: #!/bin/bash cd /home/askgelal/projects/java I saved this file as proj, changed the chmod, copied it to /usr/bin. When I call it by: proj, it does nothing. What am I doing wrong? ...

How to properly handle wildcard expansion in a bash shell script?

#!/bin/bash hello() { SRC=$1 DEST=$2 for IP in `cat /opt/ankit/configs/machine.configs` ; do echo $SRC | grep '*' > /dev/null if test `echo $?` -eq 0 ; then for STAR in $SRC ; do echo -en "$IP" echo -en "\n\t ARG1=$STAR ARG2=$2\n\n" done else ...

Why do double-quote change the result

I have a simple linux script: #!/bin/sh for i in `ls $1` do echo $i done In my temp folder are 4 file: a.a, a.aa, a.ab and a.ac When i call ./script temp/*.?? i get: temp/a.aa When i call ./script "temp/*.??" i get: temp/a.aa temp/a.ab temp/a.ac Why do the double quote change the result? ...

Unix C Shell Scripting printf help

Attempting to print out a list of values from 2 different variables that are aligned correctly. foreach finalList ($correctList $wrongList) printf "%20s%s\n" $finalList end This prints them out an they are aligned, but it's one after another. How would I have it go through each item in each list and THEN go to a new line? I want t...

Executing Shell Scripts from the OS X Dock?

I'm sure the answer is ridiculously obvious, but so far Google hasn't been very helpful. How do I set up a shell script to execute from the dock? It seems that simply creating a shortcut will open the file in my editor. Is there a flag I need to set somewhere to tell it to run instead of opening it for editing? ...

SQL select statement in UNIX inside/within IF..THEN statement.

Hi - I just want ask the steps when trying to create a simple SQL select statement in UNIX inside/within IF..THEN..FI statement. I know how to use the 'select' and 'if..then' statements in SQL*Plus, but I'm having a difficulties having a UNIX script to point to variables: If 'ABC' to 'Select...' Example: if [ "$?" = 'ABC' ] then ...

How do I translate init.d scripts from Ubuntu/Debian Linux to Solaris?

I have several init.d scripts that I'm using to start some daemons. Most of these scripts I've found on the internet and they all use start-stop-daemon. My understanding is that "start-stop-daemon" is a command that is specific to Linux or BSD distros and is not available on Solaris. What is the best way to translate my init.d scripts f...

how to loop files in linux from svn status

As being quite a newbie in linux, I have the follwing question. I have list of files (this time resulting from svn status) and i want to create a script to loop them all and replace tabs with 4 spaces. So I want from .... D HTML/templates/t_bla.tpl M HTML/templates/t_list_markt.tpl M HTML/templates/t_vip.tpl M HTML...

`(cd X; pwd)` sometimes returns two-line

I have shell script which starts with: sdir=`dirname $0` sdir=`(cd "$sdir/"; pwd)` And this usually gets expanded (with 'sh -h') into ++ dirname /opt/foo/bin/bar + sdir=/opt/foo/bin ++ cd /opt/foo/bin/ ++ pwd + sdir=/opt/foo/bin but for single user for single combination of parameters in expands into (note two lines at the result ...

Need for troubleshooting in UNIX w/ shell script program and IF..THEN command.

Since you guys are having lots of questions or not enough info to work with, below is proprietary information and as such be careful as you can. The question – (please refer to if [$7 -eq "AAA"]...then statement - in bold ) has a problem with an output, where I can only output/print count of 'AAA' regardless the variable coming in eith...

What's the Windows equivalent of a UNIX shell script?

I want to have an executable file that will call some other programs. The way I would do this in Linux is with a simple bash script that looks like this: #!/bin/bash echo "running some-program" /home/murat/some-program arg1 arg2 What's the best way to do this kind of thing under Windows? ...