bash

How do I write some (bash) shell script to convert all matching filenames in directory to command-line options?

Apparently the answer to my question "Can I restrict nose coverage output to directory (rather than package)?" is no, but I can pass a --coverage-package=PACKAGE option to nose with the package name of each .py file in the directory. So for example, if the directory contains: foo.py bar.py baz.py ...then I would need to use the comma...

Recommended online resources for learning bash scripting

When I first began bash scripting I spent a lot of time online trying to find resources for various tasks and things were scattered across tons of different forums and newsgroups. I'm not completely sure if the idea of a list of resources for those new to bash scripting is something fitting of SO's community wiki or not. I'm posting on...

A bash one-liner to change into the directory where some file is located.

I often want to change to the directory where a particular executable is located. So I'd like something like cd `which python` to change into the directory where the python command is installed. However, this is obviously illegal, since cd takes a directory, not a file. There is obviously some regexp-foo I could do to strip off the...

Why sometimes when I paste a command on my bash prompt it gets executed?

The command get executed without the need of pressing Enter. This can be dangerous sometimes... Why is that and how can I prevent it? ...

IP address and Country on same line with AWK

I'm looking for a one-liner that based on a list of IPs will append the country from where the IP is based So if I have this as and input: 87.229.123.33 98.12.33.46 192.34.55.123 I'd like to produce this: 87.229.123.33 - GB 98.12.33.46 - DE 192.34.55.123 - US I've already got a script that returns the country for IP but I need to ...

bash: save in a variable the number of seconds a process took to run

I want to run a process in bash and save in an env variable the number of seconds it took to run. How would I do such a thing? ...

Delete all files/directories except two specific directories

So, there seems to be a few questions asking about removing files/directories matching certain cases, but I'm looking for the exact opposite: Delete EVERYTHING in a folder that DOESN'T match my provided examples. For example, here is an example directory tree: . |-- coke | |-- diet | |-- regular | `-- vanilla |-- icecream | |-...

shell history question

I have two commands I execute frequently. Let's say the first is 'abcd' and the second is 'abc'. So my history contains 1000 abc arg1 arg2 arg3 1001 abcd arg1 arg2 arg3 Now if I type '!abcd' in bash, it executes the abcd command. which is fine. But if I type '!abc' in bash, it also executes the last abcd command (since it matches the...

Timing concurrent processes in bash with 'time'

Is there a simple way to do the equivalent of this, but run the two processes concurrently with bash? $ time sleep 5; sleep 8 time should report a total of 8 seconds (or the amount of time of the longest task) ...

Preserve ls colouring after grep'ing

If I do $ ls -l --color=always I get a list of files inside the directory with some nice colouring for different file types etc.. Now, I want to be able to pipe the coloured output of ls through grep to filter out some files I don't need. The key is that I still want to preserve the colouring after the grep filter. $ ls -l --color=...

Why ssh fails from crontab but succedes when executed from a command line?

I have a bash script that does ssh to a remote machine and executes a command there, like: ssh -nxv user@remotehost echo "hello world" When I execute the command from a command line it works fine, but it fails when is being executed as a part of crontab (errorcode=255 - cannot establish SSH connection). Details: ... Waiting for serve...

A good place to read & write data used by a cron script?

Hi, I'm writing a script which is to be executed hourly. It basically works as: Read the datafile if it exists. Perform an action if the datafile has certain contents. Write over or create the datafile. I will put the script in /etc/cron.hourly/ on Ubuntu which will make it execute once each hour. What would a good place to store t...

Can't redirect output to /var/lib/varlibfile, but I can copy a varlibfile to /var/lib

Hi, I have a script which is running as root, it's supposed to store it's cache file (only modified by the script) at /var/lib/varlibfile. However the script does not seem to create the file, and my investigation led me to this: $ sudo echo "something" >/var/lib/varlibfile bash: /var/lib/varlibfile: Permission denied $ sudo echo "somet...

Bash. Test for a variable unset, using a function

A simple Bash variable test goes: ${varName:? "${varName} is not defined"} I'd like to re-use this, by putting it in a function. How please? Following fails # # Test a variable exists tvar(){ val=${1:? "${1} must be defined, preferably in $basedir"} if [ -z ${val} ] then echo Zero length value else echo...

Change current directory from a script

Hi, everybody! Is it possible to change current directory from a script? I want to create a util for directory navigation in bash. I have created a test script that looks like the following: #!/bin/bash cd /home/artemb When I execute the script from the bash shell the current directory doesn't change. Is it possible at all to change...

bash: redirect and append both stdout and stderr

To redirect stdout in bash, overwriting file cmd > file.txt To redirect stdout in bash, appending to file cmd >> file.txt To redirect both stdout and stderr, overwriting cmd &> file.txt How do I redirect both stdout and stderr appending to file? cmd &>> file.txt does not work for me ...

Bash script, Illegal number: 08

I'm running a pretty simple bash script in ubuntu but have come up with a problem. If needed I'll post the whole script, but I've narrowed down the problem. Basically, I want to run some code every 15 seconds, so I started with this: time=`date +%S` time2=$((time%15)) if [ $time2 -eq 0 ] then etc, etc, etc.... The problem comes up...

How to create cronjob using bash

Does crontab have an argument for creating cronjobs without using the editor (crontab -e). If so, What would be the code create a cronjob from a bash script? ...

bash check if user mount fails

I'm writing a script to transfer some files over sftp. I wanted do the transfer as a local transfer by mounting the directory with sshfs because it makes creating the required directory structure much easier. The problem I'm having is I'm unsure how to deal with the situation of not having a network connection. Basically I need a way ...

Renaming a set of files to 001, 002, ... on Linux

I originally had a set of images of the form image_001.jpg, image_002.jpg, ... I went through them and removed several. Now I'd like to rename the leftover files back to image_001.jpg, image_002.jpg, ... Is there a Linux command that will do this neatly? I'm familiar with rename but can't see anything to order file names like this. I'm...