bash

writing a bash script that checks for a few special parameters before passing on the rest to another command

i am writing a bash script which acts as a wrapper around another command. it needs to be able to understand all of the parameters the command understands, plus a few extra. for example, if the command understands -abc switches and i wish for my wrapper script to understand those plus a -d and -e switch, then i need a way to look for and...

How to grab an arbitrary chunk from a file on unix/linux

I'm trying to copy a chunk from one binary file into a new file. I have the byte offset and length of the chunk I want to grab. I have tried using the dd utility, but this seems to read and discard the data up to the offset, rather than just seeking (I guess because dd is for copying/converting blocks of data). This makes it quite slow ...

why is screen not showing the current running process name? (Mac OSX Terminal bash)

I am running screen inside Mac OSX Terminal app (bash). Here is the screenrc (got it from here): $ cat ~/.screenrc termcapinfo xterm* ti@:te@ startup_message off defutf8 on shelltitle "$ |what?" # make screen assign window titles automatically hardstatus alwayslastline hardstatus string '%{= kw} [ %{= kb}%H%{= kw} ][%= %{= kw}%?%-Lw?...

How to make zsh run as a login shell on Mac OS X (in iTerm)?

When zsh is set as a login shell on Mac OS X, when it is started by iTerm, zsh doesn't consider that it's being run as a login shell, although it's started as ‘-zsh’ (‘-’ is put as the first character of arg[0]) which is supposed to mean that it should start as a login shell. So, when I set the login shell to bash, bash recognizes this ...

Sort file beginning at a certain line

I'd like to be able to sort a file but only at a certain line and below. From the manual sort isn't able to parse content so I'll need a second utility to do this. read? or awk possibly? Here's the file I'd like to be able to sort: tar --exclude-from=$EXCLUDE_FILE --exclude=$BACKDEST/$PC-* \ -cvpzf $BACKDEST/$BACKUPNAME.tar.gz \ /etc...

How to execute the output of a command within the current shell?

Hi, I'm well aware of the source (aka .) utility, which will take the contents from a file and execute them within the current shell. Now, I'm transforming some text into shell commands, and then running them, as follows: $ ls | sed ... | sh ls is just a random example, the original text can be anything. sed too, just an example for...

Delete all files but keep all directories in a bash script?

Hi, I'm trying to do something which is probably very simple, I have a directory structure such as: dir/ subdir1/ subdir2/ file1 file2 subsubdir1/ file3 I would like to run a command in a bash script that will delete all files recursively from dir on down, but leave all directories. Ie: di...

cat/Xargs/command VS for/bash/command

The page 38 of the book Linux 101 Hacks suggests: cat url-list.txt | xargs wget –c I usually do: for i in `cat url-list.txt` do wget -c $i done Is there some thing, other than length, where the xargs-technique is superior to the old good for-loop-technique in bash? Added The C source code seems to have only one fork. ...

A python based PowerShell ?

I just took a brief look at PowerShell (I knew it as Monad shell). My ignorant eyes see it more or less like a hybrid between regular bash and python. I would consider such integration between the two environments very cool on linux and osx, so I was wondering if it already exists (ipython is not really the same), and if not, why ? ...

Best way for testing compiled code to return expected output/errors

How do you test if compiled code returns the expected output or fails as expected? I have worked out a working example below, but it is not easily extendable. Every additional test would require additional nesting parentheses. Of course I could split this into other files, but do you have any suggestions on how to improve this?. Also I...

Syntax for a single-line BASH infinite while loop

Having trouble coming up with the right combination of semicolons and/or braces. I'd like to do this, but as a one-liner from the command line: while [ 1 ] do foo sleep 2 done ...

Compilers for shell scripts

Do you know if there's any tool for compiling bash scripts? It doesn't matter if that tool is just a translator (for example, something that converts a bash script to a C program), as long as the translated result can be compiled. I'm looking for something like shc (it's just an example -- I know that shc doesn't work as a compiler)....

Iterating over options and quoted/unquoted arguments passed to bash script

I have a bash script that uses getopt to parse its parameters. It correctly handles switches and long options, as well as arguments and quoted arguments, but I cannot figure out how to iterate over the options/arguments string returned by the backticks. Something like: params=`getopt -o ab -l ccc -- "$@"` echo $params for param in "$pa...

Check if a package is installed and then install it if it's not.

I'm working on a Ubuntu system and Currently this is what I'm doing: if ! which command > /dev/null; then echo -e "Command not found! Install? (y/n) \c" read if "$REPLY" = "y"; then sudo apt-get install command fi fi Is this what most people would do? Or is there a more elegant solution? ...

What does <() do in Bash?

In a post's answer on superuser.com, we see that join <(sort abc) <(sort bcd) will sort files abc and bcd before sending them to join. This leads to a programming question, better suited for stackoverflow. How does this work? What exactly is this <() construct? What's it called? If (sort abc) is a legal call that runs sort on abc ...

Issues with large directory and cp command

I am trying to copy all jpgs from 1 directory to another but only new files and ones that have been updated. I am using the following command: \cp -uf /home/ftpuser1/public_html/ftparea/*.jpg /home/ftpuser2/public_html/ftparea/ And I am getting the error: -bash: /bin/cp: Argument list too long I am assuming that there are 2 many ...

Find all files in a directory that are not directories themselves.

I am looking for a way to list all the files in a directory excluding directories themselves, and the files in those sub-directories. So if I have: ./test.log ./test2.log ./directory ./directory/file2 I want a command that returns: ./test.log ./test2.log and nothing else. ...

Exit status code for Expect script called from Bash

I made a Bash script which uses an expect script to automate ssh logins.The script connects to multiple servers and runs some commands. The bash script prompts for login credentials once. I want to incorporate a feature wherein the script terminates if the login fails for the first server to avoid the script checking for next servers re...

Makefile for program running on different machines

Hi, I have a C++ program that will run on several machines that use a Network File System. For each of the C++ libraries that my program uses, I installed a version for each machine, under ~/program_files/machinename/libraryname. "machinename" is obtained via bash command "hostname". On the machines I am using, "hostname" outputs someth...

read error: 0: Resource temporarily unavailable

I have a bash script which prompts for user input multiple times and processes input in the background during the time the next input is expected. I keep getting this error once in while. read error: 0: Resource temporarily unavailable I suspected the background processes in my script would be causing this so I tried putting a < dev/n...