bash

How to list variables declared in script in bash?

In my script in bash, there are lot of variables, and I have to make something to save them to file. My question is how to list all variables declared in my script and get list like this: VARIABLE1=abc VARIABLE2=def VARIABLE3=ghi Is there some EASY way for this? ...

How do I check for the existence of a file using a wildcard in Bourne shell?

What I'm looking for is something like: if [ -f "filenam*" ]; then ... fi But the wildcard (*) causes problems. I've also tried: if [ `ls filenam* > /dev/null 2>&1` ]; then ... fi Which may work in other shells but doesn't appear to work in Bourne shell (sh). Thanks for any help! EDIT: Sorry, not C shell, Bourne shell (s...

Shellwords.shellescape implementation for Ruby 1.8

While the build of 1.8.7 I have seems to have a backported version of Shellwords::shellescape, I know that method is a 1.9 feature and definitely isn't supported in earlier versions of 1.8. Does anyone know where I can find, either in Gem form or just as a snippet, a robust standalone implementation of Bourne-shell command escaping for ...

OS X bash: dirname

I want to create a simple bash script to launch a Java program on OS X. The names of the file, the file path, and the immediate working folder all contain spaces. When I do this: #!/bin/sh cd `dirname $0` I get usage: dirname path I have also tried putting quotes in all kinds of different places. The most elaborate example being c...

How to embed bash script directly inside a git alias

Can I embed the following bash shell code: for name in $(git diff --name-only $1); do git difftool $1 $name & done directly into the creation of a git alias: git config --global alias.diffall ***my-bash-code-here*** This leads on from my previous question/answer on SO, where I put the code into a .sh file and then aliased to the f...

How to do something to every file in a directory using bash?

I started with this: command * But it doesn't work when the directory is empty; the * wildcard becomes a literal "*" character. So I switched to this: for i in *; do ... done which works, but again, not if the directory is empty. I resorted to using ls: for i in `ls -A` but of course, then file names with spaces in them get s...

How to test filename expansion result in bash?

I want to check whether a directory has files or not in bash. My code is here. for d in {,/usr/local}/etc/bash_completion.d ~/.bash/completion.d do [ -d "$d" ] && [ -n "${d}/*" ] && for f in $d/*; do ...

Using rsync to rename files during copying with --files-from ?

Using rsync, how can I rename files when copying with the --files-from argument? I have about 190,000 files, each of which need to be renamed when copying from source to destination. I plan to have the list of files in a text file to pass to the --files-from argument. ...

BASH copy all files except one.

I would like to copy all files out of a dir except for one name Default.png. It seems that there are a number of ways to do this. What seems the most effective to you? ...

Reorganize folders with a script

Hi, Through the last years I have different types of software to organize my photo and music collections. This has resulted in differences in directory structure, and I'd like this to be uniform. Problem a: Music collection I want my music collection to look like: /Artist/Album/files.* but now there are several instances of /A/Artist/...

Bash and Test-Driven Development

When writing more then a trivial script in bash, I often wonder how to make the code testable. It is typically hard to write test for bash code, due to the fact that it is low on functions that take value and return a value, and high on functions that check and set some aspect in the environment, modify the file-system, invoke a program...

How do I select a subset of lines from the history in bash?

Quite often I grep through my bash shell history to find old commands, filepaths, etc. Having identified the history number of interest, I would like to see a few lines of context on either side, i.e. view a subset of history lines. For example: $ history | grep ifconfig 8408 ifconfig eth0 8572 sudo ifconfig eth0 down I would like ...

Bash Scripting - How to set the group that new files will be created with?

I'm doing a bash shell script and I want to change the default group that new files are created as. I know you use umask to change the permissions. Is there something for the group? ...

bash: how to change the basename only of a list of files

Hi all, I have a lit of files (which I get from find bla -name "*.so") such as: /bla/a1.so /bla/a2.so /bla/blo/a3.so /bla/blo/a4.so /bla/blo/bli/a5.so and I want to rename them such as it becomes: /bla/liba1.so /bla/liba2.so /bla/blo/liba3.so /bla/blo/liba4.so /bla/blo/bli/liba5.so ... i.e. add the prefix 'lib' to the basename any ...

get ouput from shell script back into a makefile

I've tried something like this: VAR := $(echo \`find . -name ".txt"`) but when I echo $VAR from inside a target, I get nothing... ...

How to redirect output of an already running process.

Normally I would start a command like longcommand &; I know you can redirect it by doing something like longcommand > /dev/null; for instance to get rid of the output or longcommand 2>&1 > output.log to capture output. But I sometimes forget, and was wondering if there is a way to capture or redirect after the fact. longcomma...

MySQL from the command line - can I practically use LOCKs?

I'm doing a bash script that interacts with a MySQL datatabase using the mysql command line programme. I want to use table locks in my SQL. Can I do this? mysql -e "LOCK TABLES mytable" # do some bash stuff mysql -u "UNLOCK TABLES" The reason I ask, is because table locks are only kept for the session, so wouldn't the lock be released...

What is the safest way to initialize bash arrays with quoted values from function output?

I prefer to program my bash scripts to be as procedural as possible. One difficulty I've encountered in trying to do so happens when passing array data between functions, a task that's not well supported in bash. As an example, it's trivial to initial an array in bash with multiple hard-coded, quoted values, each of which may contain m...

Sorting words (not lines) in VIM

The built-in VIM :sort command sorts lines of text. I want to sort words in a single line, e.g. transform the line b a d c e f to a b c d e f Currently I accomplish this by selecting the line and then using :!tr ' ' '\n' | sort | tr '\n' ' ', but I'm sure there's a better, simpler, quicker way. Is there? Note that I use bash so if...

Using regular expressions with 'msggrep'

Hello, I have a PO file with a content like: msgid "or" msgstr "or-translation" msgid "orand" msgstr "orand-translation" I need to obtain the translation of a given msgid. Using the command "msggrep -K -e 'orand' template2.pot" I get the translation for 'orand', and this is ok. But when I use "msggrep -K -e 'or' template2.pot" if...