bash

Interesting usage of tar... but what is happening?

I saw the following interesting usage of tar in a co-worker's Bash scripts: `tar cf - * | (cd <dest> ; tar xf - )` Apparently it works much like rsync -av does, but faster. The question arises, how? -m EDIT: Can anyone explain why should this solution be preferable over the following? cp -rfp * dest Is the former faster? ...

How do you use cat recursively?

Suppose I want to count the lines of code in a project. If all of the files are in the same directory I can execute: cat * | wc -l However, if there are sub-directories, this doesn't work. For this to work cat would have to have a recursive mode. I suspect this might be a job for xargs, but I wonder if there is a more elegant solution...

trying to capture javac output in bash shell

I'm trying to redirect the java compiler output to a file. I thought it's supposed to be: javac file.java > log.txt or something. Instead, I see all the output on the terminal and nothing in log.txt! Also, if I want to log errors too, do I do javac file.java 2>&1 > log.txt ? ...

How do I open a Finder sidebar folder via AppleScript?

I have a bash script which will be run on a Mac via ssh. The script requires a particular network drive to already be mounted. On the Mac, I mount this drive by opening a folder "JPLemme" on that drive in Finder. This mounts the drive until the Mac goes to sleep at night. Obviously, Finder isn't available via ssh, so I want to create an...

How to print the Nth column of a text file with AWK using argv

Suppose I have a text file with data separated by whitespace into columns. I want to write a little shell script which takes as input a filename and a number N and prints out only that column. With awk I can do the following: awk < /tmp/in '{print $2}' > /tmp/out This code prints out the second column. But how would one wrap that in...

echo "-e" doesn't print anything

I'm using GNU bash, version 3.00.15(1)-release (x86_64-redhat-linux-gnu). And this command: echo "-e" doesn't print anything. I guess this is because "-e" is one of a valid options of echo command because echo "-n" and echo "-E" (the other two options) also produce empty strings. The question is how to escape the sequence "-e" for ec...

bash if -a vs -e option

Both about "-a" and "-e" options in Bash documentation (http://www.gnu.org/software/bash/manual/bashref.html#Bash-Conditional-Expressions) is said: -a file True if file exists. -e file True if file exists. Trying to get what the difference is I ran the following script: resin_dir=/Test/Resin_wheleph/Results if [ -e ${resin_d...

Compiling and Running java in Unix ( coming from Windows )

Ok, this is working on windows. My Java app is running and functioning normally javac -classpath .;ojdbc14.jar -g foo.java java -classpath .;ojdbc14.jar foo However, when I do the same thing on Unix I get this error: ojdbc14.jar: not found What am I doing wrong? I know the ";" is telling my shell that ojdbc14.jar is a new comma...

How to close a file descriptor from another process in unix systems

You can use command lsof to get file descriptors for all running processes, but what I would like to do is to close some of those descriptors without being inside that process. This can be done on Windows, so you can easily unblock some application. Is there any command or function for that? ...

remove whitespace from bash variable

Assuming var contains spaces, newlines, and tabs followed by some text, why does ${var#"${var%%[![:space:]]*}"} # strip var of everything # but whitespace # then remove what's left # (i.e. the whitespace) from var remove the white spac...

race condition in the common lock on file?

this is the standard approach to create locks using file system. For example, visudo uses it: [ -f ".lock" ] && exit 1 touch .lock # do something rm .lock 1) I'm confused, for there's a race condition, yet Linux uses it 2) is there a better way to lock on files from shell? 3) or do I have to use directories instead? Found solutio...

How can I get my bash script to work?

My bash script doesn't work the way I want it to: #!/bin/bash total="0" count="0" #FILE="$1" This is the easier way for FILE in $* do # Start processing all processable files while read line do if [[ "$line" =~ ^Total ]]; then tmp=$(echo $line | cut -d':' -f2) count=$(expr $count + 1)...

Scripting language choice for initial performance

I have a small lightweight application that is used as part of a larger solution. Currently it is written in C but I am looking to rewrite it using a cross-platform scripting language. The solution needs to run on Windows, Linux, Solaris, AIX and HP-UX. The existing C application works fine but I want to have a single script I can maint...

Script is re-reading arguments

When I supply the script with the argument: hi[123].txt it will do exactly what I want. But if I specify the wildcard character ( hi*.txt ) it will be re-reading some files. I was wondering how to modify this script to fix that silly problem: #!/bin/sh count="0" total="0" FILE="$1" #FILE specification is now $1 Specification.. for F...

How do I append onto pipes?

So my question is if I can somehow send data to my program and then send the same data AND its result to another program without having to create a temporary file (in my case ouputdata.txt). Preferably using linux pipes/bash. I currently do the following: cat inputdata.txt | ./MyProg > outputdata.txt cat inputdata.txt outputdata.txt |...

How to make ssh to kill remote process when I interrupt ssh itself?

In a bash script I execute a command on a remote machine through ssh. If user breaks the script by pressing Ctrl+C it only stops the script - not even ssh client. Moreover even if I kill ssh client the remote command is still running... How can make bash to kill local ssh client and remote command invocation on Crtl+c? A simple script:...

Shell Script doesn't run automatically though it is registered in Mac OS X Login Items

I have the following shell script registered in my "Login Items" preferences but it does not seem to have any effect. It is meant to launch the moinmoin wiki but only works when it is run by hand from a terminal window, after which it runs until the machine is next shut down. #!/bin/bash cd /Users/stuartcw/Documents/Wiki/moin-1.7.2 /usr...

Prevent duplicates from being saved in bash history

I'm trying to prevent bash from saving duplicate commands to my history. Here's what I've got: shopt -s histappend export HISTIGNORE='&:ls:cd ~:cd ..:[bf]g:exit:h:history' export HISTCONTROL=erasedups export PROMPT_COMMAND='history -a' This works fine while I'm logged in and .bash_history is in memory. For example: $ history 1 vi...

How can I remove the first line of a text file using bash/sed script?

I need to repeatedly remove the first line from a huge text file using a bash script. Right now I am using sed -i -e "1d" $FILE - but it takes around a minute to do the deletion. Is there a more efficient way to accomplish this? ...

How to bind a key to sigkill in bash?

I'm developing my application (on Linux) and sadly it sometimes hangs. I can use Ctrl+C to send sigint, but my program is ignoring sigint because it's too far gone. So I have to do the process-killing-dance: Ctrl+Z $ ps aux | grep process_name $ kill -9 pid Is there a way to configure bash to send the kill signal to the current proces...