bash

How to determine file type in bash script?

I am writing a nightly build script in bash. Everything is fine and dandy except for one little snag: #!/bin/bash for file in "$PATH_TO_SOMEWHERE"; do if [ -d $file ] then # do something directory-ish else if [ "$file" == "*.txt" ] # this is the snag then ...

[bash] Fill placeholders in file in single pass

I have a skeleton text file with placeholder strings: blah blah blah blah $PLACEHOLDER_1$ blah $PLACEHOLDER_2$ and so on. Specific "form" of placeholders does not matter -- I may change them to whatever most comfortable for specific implementation. I have a bash script where I know values for placeholders, and I need to generate a ne...

[bash] Escape a string for sed search pattern

In my bash script I have an external (received from user) string, which I should use in sed pattern. REPLACE="<funny characters here>" sed "s/KEYWORD/$REPLACE/g" How can I escape the $REPLACE string so it would be safely accepted by sed as a literal replacement? NOTE: The KEYWORD is a dumb substring with no matches etc. It is not sup...

Date and time calculations to track time using command line in Unix/Linux

I'm looking for an easy way to track time I work on a project using the command line. I do echo "$(date) : Start" >>worktime.txt When I start working and echo "$(date) : End" >>worktime.txt When I stop working. The result is a file of the form: kent@rat:~/work$ cat worktime.txt Fri Jan 2 19:17:13 CET 2009 : Start Fri Jan 2 19:4...

Compare integer in bash, unary operator expected

The following code gives [: -ge: unary operator expected when i=0 if [ $i -ge 2 ] then #some code fi why? ...

How do I tell rsync to run only if the destination directory exists?

I have this bash script running my backup to an external hard drive... only if that drive is mounted (OS X): DIR=/Volumes/External; if [ -e $DIR ]; then rsync -av ~/dir_to_backup $DIR; else echo "$DIR does not exist"; fi This works, but I sense I am misreading the rsync man page. Is there a builtin rsync option to abort the run i...

build argument lists containing whitespace

In bash one can escape arguments that contain whitespace. foo "a string" This also works for arguments to a command or function: bar() { foo "$@" } bar "a string" So far so good, but what if I want to manipulate the arguments before calling foo? This does not work: bar() { for arg in "$@" do args="$args \"pre...

A problem with folding bash functions in vim

Hello! I have a bash script file which starts with a function definition, like this: #!/bin/bash # ..... # ..... function test { ... ... } ... ... I use vim 7.2, and I have set g:sh_fold_enabled=1 such that folding is enabled with bash. The problem is that the folding of the function test is not ended correctly, i.e. it lasts until t...

Why is 'cp' failing in this shell script?

Here's a snippet of code from a shell script I have written: for src in $(find . -type f -maxdepth 1 \! -name ${deploy} \! -name gvimrc) do src=$(basename ${src}) dest="~/.${src}" copy="${src} -> ${dest}" cp $src $dest && echo -e "${ok} ${copy}" || echo -e "${fail} ${copy}" done For some reason, cp fails to execute. Fo...

How can I select random files from a directory in bash?

I have a directory with 2000 files or so. I want a script or a list of piped commands that will allow me to select a random sample of N files. ...

What's the difference between .bashrc, .bash_profile, and .environment?

I've used a number of different *nix-based systems of the years, and it seems like every flavor of Bash I use has a different algorithm for deciding which startup scripts to run. For the purposes of tasks like setting up environment variables and aliases and printing startup messages (e.g. MOTDs), which startup script is the appropriate...

How to repace ${} variables in a *nix text file

I want to pipe the output of a "template" file into mysql, the file having variables like ${dbName} interspersed. What is the commandline utility to replace these instances and dump the output to stdout? ...

How can I suppress the output due to a SIGSEGV or a SIGFPE in a Fortran 90 program?

Hello, I have a bash script that runs a simulation program written in Fortran 90, and all output is redirected to a file. If the program finishes without problems, I set a success parameter. The code looks something like this: #!/bin/bash ... echo -n "Running program..." ./sim_program >...

Stop echoing ^C to terminal

How do I tell my bash to not echo ^C back to terminal? If I just hit Ctrl+C in bash session, nothing is printed in my terminal window. But if I terminate some program with Ctrl+C, sometimes ^C is echoed and printed in my terminal. Is there any way to tell my bash I do not want echoing back ^C at all? ...

How to redirect output to a file and stdout

In bash, calling foo would display any output from that command on the stdout. Calling foo > output would redirect any output from that command to the file specified (in this case 'output'). Is there a way to redirect output to a file and have it display on stdout? ...

Append text to stderr redirects in bash

Right now I'm using exec to redirect stderr to an error log with exec 2>> ${errorLog} The only downside is that I have to start each run with a timestamp since exec just pushes the text straight into the log file. Is there a way to redirect stderr but allow me to append text to it, such as a time stamp? ...

How can a bash script know the directory it is installed in when it is sourced with . operator?

What I'd like to do is to include settings from a file into my current interactive bash shell like this: $ . /path/to/some/dir/.settings The problem is that the .settings script also needs to use the "." operator to include other files like this: . .extra_settings How do I reference the relative path for .extra_settings in the .setti...

Removing directories with spaces with bash

I'm using the following command to remove every ".dummy" directories in a folder named "Test Folder": rm -rf `find "Test Folder" -type d -name .dummy` However, this doesn't work since it expands to, say: rm -rf Test Folder/.dummy Since the whitespace is not escaped, this fails. I also tried something like this: find "Test Folder"...

Bash Shell - What is equivalent of DOS shell F8 ?

When working an interactive bash session, one aspect from the Windows shell I miss is the F8 key where you start typing a command, hit F8 and the shell finds the most recent command entered in history that matches what you have typed so far. e.g. me@Ubntu07:~>cd /home/jb<F8 Key Here> brings up my prior command: me@Ubntu07:~>cd /home/...

How do I (successfully) decode a encoded password from command line openSSL?

Using PyCrypto (although I've tried this in ObjC with OpenSSL bindings as well) : from Crypto.Cipher import DES import base64 obj=DES.new('abcdefgh', DES.MODE_ECB) plain="Guido van Rossum is a space alien.XXXXXX" ciph=obj.encrypt(plain) enc=base64.b64encode(ciph) #print ciph print enc outputs a base64 encoded value of : ESzjTnGMRFnfV...