bash

Bash: How to do a variable expansion within an arithmetic expression?

While attempting to write a simple bash script to help with my deployment process, I ran in to a confusing error: #!/bin/bash WEEKDAY=$(date +'%u') echo $WEEKDAY DAYS_TO_WEDNESDAY=$((3-$WEEKDAY)) echo $DAYS_TO_WEDNESDAY Results in: 1 ")syntax error: invalid arithmetic operator (error token is " The strangest part of it is that I c...

Correct Bash and shell script variable capitalization

I run across many shell scripts with variables in all caps, and I've always thought that there is a severe misunderstanding with that. My understanding is that, by convention (and perhaps by necessity long ago), environment variables are in all-caps. But in modern scripting environments like Bash, I have always prefered the convention ...

In bash, how to make control-delete mean kill-word?

Bash uses readline, and readline can delete the word to the right of the cursor with "kill-word". The problem is in recognizing the keypress of control-delete. When I press them in bash, "5~" is output on the screen. I could just bind for this, but it would mean that one day I need to type "5~", and it deletes a word to the right in...

linux: redirect stdout after the process started

I have a process that already started, with stdout to the console. can I redirect it to a file, without restarting it? ...

How to use "xargs" properly when argument list is too long

Can someone please give me an example of using xargs on the below operation? tar c $dir/temp/*.parse\ | lzma -9 > $dir/backup/$(date '+%Y-%m-%d')-archive.tar.lzma I get the error from bash "/bin/tar: Argument list too long" Particularily I am trying to do LZMA compression on about 4,500 files; so this isn't surprising. I just do...

Why can I run a Bash function with the 000 permissions?

It is not totally rigth that I can run a Bash function with 000 permissions, but almost. My code is: #!/bin/bash function hello { echo Hello! } The hello-file has permissions: -r-------- 1 UnixBasics hello_file Firtsly, I type with the current permissions: $ . ./hello_file;hello The tweak is to change the 400 permissions to 00...

bash stacktrace

I need something like a stacktrace in bash, is that possible? A script is misbehaving. I need to know who calls that script, and who calls the calling script, and so on, only by modifying the misbehaving script. What do you say? ...

Removing created temp files in unexpected bash exit

I am creating temporary files from a bash script. I am deleting them at the end of the processing, but since the script is running for quite a long time, if I kill it or simply CTRL-C during the run, the temp files are not deleted. Is there a way I can catch those events and clean-up the files before the execution ends? Also, is there s...

Timeout a command in bash without unnecessary delay

This answer to a similar question proposes a 1-line method to timeout a long-running command from the bash command line: ( /path/to/slow command with options ) & sleep 5 ; kill $! But it's possible that a given "long-running" command may finish earlier than the timeout. (Let's call it a "typically-long-running-but-sometimes-fast" comm...

Associative arrays in Shell scripts

We required a script that simulates Associative arrays or Map like data structure for Shell Scripting, any body? ...

Why can't I use job control in a bash script?

In this answer to another question, I was told that in scripts you don't have job control (and trying to turn it on is stupid) This is the first time I've heard this, and I've pored over the bash.info section on Job Control (chapter 7), finding no mention of either of these assertions. [Update: The man page is a little better, me...

In what order should I send signals to gracefully shutdown processes?

In a comment on this answer of another question, the commenter says: don’t use kill -9 unless absolutely necessary! SIGKILL can’t be trapped so the killed program can’t run any shutdown routines to e.g. erase temporary files. First try HUP (1), then INT (2), then QUIT (3) I agree in principle about SIGKILL, but the rest i...

What are replacement and new practices from sh to Bash?

As someone who did a lot of sh scripting twenty years ago, and now coming back to it again, I find I'm using techniques that are considered obsolete. I should take the time to read the "What's new", but I don't. and it's not terribly efficient. Examples: Instead of use tmpfile=/tmp/me$$ tmpfile=`mktemp` [ ] ...

Should I put complex functionality in the shell login script or separate programs?

I have quite a few simple functions that I had previously kept in my .profile but I decided to put them in Perl scripts and add aliases to the Perl scripts. I feel like this is a bad idea, but the functionality looks/is better in Perl than bash since it is fairly complex (involving floating point math etc.). Are there any best practices...

Bash script to find a file in directory tree and append it to another file

The title is rather more simplified than the functionality I am trying to express in a script. I have a one-level deep directory tree (much bigger than example) which contain various content, although only two particular files are of interest to me. A file called 'current' and another called 'revisions' - foo | |-> current - ba...

How do I write stderr to a file while using "tee" with a pipe?

Hi, I have the below command line argument which will print the output of aaa.sh to the screen while also writing stdout to bbb.out; however I would also like to write stderr to a file ccc.out. Any suggestions on how to modify the below piece? Thanks! ./aaa.sh | tee ./bbb.out Update: stdout and stderr should still both be printed to ...

How can I capture the text between specific delimiters into a shell variable?

Hello, I have little problem with specifying my variable. I have a file with normal text and somewhere in it there are brackets [ ] (only 1 pair of brackets in whole file), and some text between them. I need to capture the text within these brackets in a shell (bash) variable. How can I do that, please? ...

Why does wdiff not work with named pipes

How come I can do this in bash: $ diff -u <(echo -e "line1\nline2") <(echo -e "line1\nline3") --- /dev/fd/63 2009-03-30 09:49:07.527272646 +0100 +++ /dev/fd/62 2009-03-30 09:49:07.527272646 +0100 @@ -1,2 +1,2 @@ line1 -line2 +line3 i.e. I can use named pipes / process substituion to get the diff of a small chunk of text. However wh...

How do I write a bash script to restart a process if it dies?

I have a python script that'll be checking a queue and performing an action on each item: # checkqueue.py while True: check_queue() do_something() How do I write a bash script that will check if it's running, and if not, start it. Roughly the following pseudo code (or maybe it should do something like ps | grep?): # keepalivescr...

abstracting the conversion between id3 tags, m4a tags, flac tags...

I'm looking for a resource in python or bash that will make it easy to take, for example, mp3 file X and m4a file Y and say "copy X's tags to Y". Python's "mutagen" module is great for manupulating tags in general, but there's no abstract concept of "artist field" that spans different types of tag; I want a library that handles all the ...