bash

Extremly Newbie - bash script

I want to create a extremely simple bash script, (my_copy.sh), that reads arbitrary number input files, a destination directory, and finally asks for confirmation if you want to perform the copy. Example usage: ./my_copy.sh Type in the file names to copy: file1 file2 Anna Kurt Arne Type in the directory to copy to: dir_3 Are...

Search and replace in Shell

I am writing a shell (bash) script and I'm trying to figure out an easy way to accomplish a simple task. I have some string in a variable. I don't know if this is relevant, but it can contain spaces, newlines, because actually this string is the content of a whole text file. I want to replace the last occurence of a certain substring w...

How to terminate script's process tree in Cygwin bash from bash script

I have a Cygwin bash script that I need to watch and terminate under certain conditions - specifically, after a certain file has been created. I'm having difficulty figuring out how exactly to terminate the script with the same level of completeness that Ctrl+C does, however. Here's a simple script (called test1) that does little more t...

Insert Command into Bash Shell

Is there any way to inject a command into a bash prompt in Linux? I am working on a command history app - like the Ctrl+R lookup but different. I am using python for this. I will show a list of commands from history based on the user's search term - if the user presses enter, the app will execute the command and print the results. So fa...

Filter out find results where I may not read file / see directory contents

Example: find / * Gives me all files and directories, but I want only those files I may read and those directories I may see the contents of. Otherwise I get problems as when I try to find file information for all files: for i in ls $( find / * ); do file $i; done Which results in: find: /lost+found: Permission denied find: /proc/...

Find and Replace Inside a Text File from a Bash Command

Whats the simplest way to do a find and replace for a given input string, say "abc", and replace with another string, say "XYZ" - the the file, /tmp/file.txt? I am wrtting an app and using IronPython to execute commands through SSH - but I dont know unix that well and dont know what I am looking for. I have heard that Bash script, apar...

Echo spaces in bash script

How do I echo one or more tab characters using a bash script? When I run this code res=' 'x # res = "\t\tx" echo '['$res']' # expect [\t\tx] I get this res=[ x] # that is [<space>x] ...

How do I limit the running time of a BASH script.

I have a long running BASH script that I am running under CYGWIN on Windows. I would like to limit the script to run for 30 seconds, and automatically terminate if it exceeds this limit. Ideally, I'd like to be able to do this to any command. For example: sh-3.2$ limittime -t 30 'myscript.sh' or sh-3.2$ limittime -t 30 'grep func *...

How do you set a directory to have persistent group permissions?

We have two users: user1 user2 They both belong to the group 'admin'. We have a directory that has been set to 775. The directory's group has been changed to 'admin'. Each user has full access to write into that directory, though when a user writes a new file to the directory, the group permissions of the folder are not persisted to...

How to escape extended pathname expansion patterns in quoted expressions?

In addition to the basic *, ? and [...] patterns, the Bash shell provides extended pattern matching operators like !(pattern-list) ("match all except one of the given patterns"). The extglob shell option needs to be set to use them. An example: ~$ mkdir test ; cd test ; touch file1 file2 file3 ~/test$ echo * file1 file2 file3 ~/test$ sh...

SVN resolve multiple files using bash/terminal on OS X

I have a lot of conflicts in a working copy that has been merged to, I know that each of them want to be resolved to the right side of the merge. Is it possible to do this in bash/terminal on OSX, for example (in pseudo code:) find . -name '*.merge-right.*' rm original-filename.filetype cp original-filename.filetype.merge-right.r...

Programming a terminal emulator, what's in it?

This is related somewhat to this question about a better shell terminal/gui-interface for cmd.exe In my quest to find a better shell terminal, the only useful thing I came across was Console2, other alternatives weren't free and generally didn't offer much more than Console2 to make them worth their price. I can't help but wonder, "how...

The sort -R command doesn't sort lines randomly in Linux

I can't get --random-sort to work with the sort command on a Fedora Linux-system. Some context information: $ cat /etc/fedora-release Fedora release 7 (Moonshine) $ which sort /bin/sort $ man sort | grep -A 2 '\-R' -R, --random-sort sort by random hash of keys $ man sort | grep -A 3 '\-R' -R, --random-sort ...

How to list running screen sessions ?

I have a bunch of servers, on which I run experiments using screen. The procedure is the following : ssh to server XXX launch screen start experiments in a few tabs detach screen disconnect from the server While the experiments are running, I can easily find on which servers they are by sshing to all servers and listing my running pr...

Uses for this bash filename extraction technique?

I have a portion of a bash script that is getting a filename without extension, but I'm trying to understand what is really going on here. What are the "%%"'s for? Can someone elaborate on what bash is doing behind the scenes? How can this technique be used on a general basis? #!/bin/bash for src in *.tif do txt=${src%%.*} ...

How do I recursively list all directories at a location, breadth-first?

Breadth-first list is important, here. Also, limiting the depth searched would be nice. $ find . -type d /foo /foo/subfoo /foo/subfoo/subsub /foo/subfoo/subsub/subsubsub /bar /bar/subbar $ find . -type d -depth /foo/subfoo/subsub/subsubsub /foo/subfoo/subsub /foo/subfoo /foo /bar/subbar /bar $ < what goes here? > /foo /bar /foo/subfoo...

Bash - Passing arguments by reference

Hi all, I want to ask if it is possible to pass arguments to a script function by reference: i.e. to do something that would look like this in C: Void boo (int & myint) { myint= 5; } main (){ int t= 4; printf t; // t->4 boo (t); printf t; // t ->5 } So then in BASH I want to do somthing like: Function boo () { ...

How do I redirect stdin from a shell script to a command in the shell script?

How do I redirect stdin from a shell script to a command in the shell script? I am trying to pass the stdin to a java System.in stream. I want to replace find . -type f | $JAVA_HOME/bin/java com.domain.BatchProcess with find . -type f | ./batch.sh ...

How do you start Unix screen command with a command?

According to the docs for the Unix "screen" command, you can configure it in .screenrc to start with a bunch of default screens, each running a command that you specify. Here's my cofig: # Default screens screen -t "shell_0" 1 screen -t "autotest" 2 cd ~/project/contactdb ; autotest It will not run the autotest command. That window ...

-mtime FIles older than 1 hour

Hello all, I have this command that I run every 24 hours currently. find /var/www/html/audio -daystart -maxdepth 1 -mtime +1 -type f -name "*.mp3" -exec rm -f {} \; I would like to run it every 1 hour and delete files that are older than 1 hour. Is this correct: find /var/www/html/audio -daystart -maxdepth 1 -mtime +0.04 -type f -...