shell-scripting

How do I get both STDOUT and STDERR to go to the terminal and a log file?

I have a script which will be run interactively by non-technical users. The script writes status updates to STDOUT so that the user can be sure that the script is running OK. I want both STDOUT and STDERR redirected to the terminal (so that the user can see that the script is working as well as see if there was a problem). I also want b...

shell script for replacing some files with symlinks

Some servers only support ftp to upload files. When I export the a project from my subversion repository to my windows machine, all (linux) symlinks are replaces by placeholder-files: link ../www_public/images after uploading the all exported files I now use find | xargs grep -P ^link to find all those placeholders. I then repla...

What setting in vim counteracts smartindent's refusal to indent # comments in shell scripts?

I recently started using vim 7 (previously vim 6) and the smartindent setting. For the most part, it works well, though I'm so used to typing a tab after an open brace that it is almost counter-productive. However, there is one piece of maniacal behaviour. When editing a shell script, I try to create a comment at the current indent le...

Get program execution time in the shell

I want to execute something in a linux shell under a few different conditions, and be able to output the execution time of each execution. I know I could write a perl or python script that would do this, but is there a way I can do it in the shell? (which happens to be bash) ...

Is there a way to check if an USB drive is stopped?

I've written a script to backup my server's HD every night. At the end of the script, I sync, wait a couple of minutes, sync and then I issue sg_start --stop to stop the device. The idea is to extend the lifetime of the device by switching the HD off after ten minutes of incremental backup (desktop disks will survive several thousand on/...

Best way to kill all child processes

I basically want to kill a whole process tree. What is the best way to do this using any common scripting languages. I am looking for a simple solution. ...

Input from within shell script

I have a script that calls an application that requires user input, e.g. run app that requires user to type in 'Y' or 'N'. How can I get the shell script not to ask the user for the input but rather use the value from a predefined variable in the script? In my case there will be two questions that require input. ...

Shell script to compare dates from multiple files in Linux

I have lots of home directories under /ifshome on Linux. I want to see which users have not logged in for the past 6 months, and my solution is to parse the /ifshome/user/.lastlogin file. Each .lastlogin file has the same format, 1 line: Last Login: Fri Mar 09 18:06:27 PST 2001 I need to build a shell script that can parse the .lastlo...

Using getopts in bash shell script to get long and short command line options

I wish to have long and short forms of command line options invoked using my shell script. I know that getopts can be used, but like in Perl, I have not been able to do the same with shell. Any ideas on how this can be done, so that i can use options like: ./shell.sh --copyfile abc.pl /tmp/ ./shell.sh -c abc.pl /tmp/ In the above, bo...

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? ...

write to fifo/pipe from shell, with timeout

I have a pair of shell programs that talk over a named pipe. The reader creates the pipe when it starts, and removes it when it exits. Sometimes, the writer will attempt to write to the pipe between the time that the reader stops reading and the time that it removes the pipe. reader: while condition; do read data <$PIPE; do_stuff; done...

Extract substring in bash

Looking for a solution in bash (will be part of a script). Given a filename in the form "someletters_12345_moreleters.ext", I want to extract the 5 digits and put them into a variable. So to emphasize the point. I have a filename with x number of characters then a five digit sequence surrounded by a single underscore on either side th...

script to manipulate the dhcp/conf

i want to write a script to manipulate the dhcp.conf file. which in the sense, it has to read the file and it should ping all the lease IP and should give another text file, in which it should give the list of IPs, which devices are now existing. EDIT: thanks for your swift reply. dhcpd.conf file is like this lease 172.31.0.10 { some...

Counting no. of Delimiter in a row in a File in Unix

I have a file 'records.txt' which contains over 200,000 records. Each record is on a separate line and has multiple fields separated by a delimiter '|'. Each row should have 35 fields, but the problem is one of these rows has <>35 fields, i.e. <>35 '|' characters. Can someone please suggest a way in Unix, by which I can identify the r...

Linux command to sum integers, one per line?

I am looking for a command that will accept as input multiple lines of text, each line containing a single integer, and output the sum of these integers. As a bit of background, I have a log file which includes timing measurements, so through grepping for the relevant lines, and a bit of sed reformatting I can list all of the timings in...

format bash variable for command

From my bash shell I would like to call a program n times with a different numbered parameter, which has to be in a fixed format like "%02i" One way would be: for ((i=23; i<42;i++)); do sh ../myprogram `printf "%02i\n" $i` done Is there a way to improve the printf.. part? I believe this might be a performance bottleneck with mor...

Printing a series of images to exact dimensions

I'd like to script printing of a series of photographs, but to exact dimensions. (e.g. all jpegs in dir /tmp/printing, at 1.5" x 1.0", 6 to a page) I happen to have my hands on a Ubuntu Linux box and a deskjet printer (hp5150) but could probably use Windows or possibly get another printer if absolutely required. I'm reasonably familiar...

Parallel processing from a command queue on Linux (bash, python, ruby... whatever)

I have a list/queue of 200 commands that I need to run in a shell on a Linux server. I only want to have a maximum of 10 processes running (from the queue) at once. Some processes will take a few seconds to complete, other processes will take much longer. When a process finishes I want the next command to be "popped" from the queue a...

Bash script: get sublist of file

Hi, I store the SQL script for a particular release in a subdirectory of 'scripts' named after the release version, e.g. ... ./scripts/1.8.3/script-1.8.3.sql ./scripts/1.8.4/script-1.8.4.sql ./scripts/1.8.4.1/script-1.8.4.1.sql ./scripts/1.8.4.2/script-1.8.4.2.sql ./scripts/1.8.4.3/script-1.8.4.3.sql ./scripts/1.9.0/script-1.9.0.sql ./...

MacFUSE: Keepalive?

Is there a way to configure MacFUSE to keep a mounted volume alive if the remote host is set to break the connection due to inactivity? If there is no direct way to configure this, would there be a way to write a script to accomplish this? ...