bash

Setting environment variables in linux using bash

In tcsh, I have the following simple script working: #!/bin/tcsh setenv X_ROOT /some/specified/path setenv XDB ${X_ROOT}/db setenv PATH ${X_ROOT}/bin:${PATH} xrun -d xdb1 -i $1 > $2 My question is, what is the equivalent to the tcsh setenv function in bash? Is there a direct analog? The environment variables are for locating ...

Unable to cat Ruby blocks to files

Please help, I am stuck here --- irb> a = "line of text\n line two\n line three" irb> system("cat > test_file << #{a}") cat: of: No such file or directory cat: text: No such file or directory => false ...

How to add a progress bar to a bash script?

When scripting in bash or any other shell in *NIX, while running a command that will take more than a few seconds, a prgress bar is needed. For example, copying a big file, opening a big tar file. What ways do you recommend to add progress bars to bash scripts? ...

truncate output in BASH

How do I truncate output in BASH? For example, if I "du file.name" how do I just get the numeric value and nothing more? later addition: all solutions work perfectly. I chose to accept the most enlightning "cut" answer because I prefer the simplest approach in bash files others are supposed to be able to read. ...

What's the best practice for changing working directories inside scripts?

Do you think changing directories inside bash or Perl scripts is acceptable? Or should one avoid doing this at all costs? What is the best practice for this issue? ...

How can I automate running commands remotely over SSH?

I've searched around a bit for similar questions, but other than running one command or perhaps a few command with items such as: ssh user@host -t sudo su - However, what if I essentially need to run a script on (let's say) 15 servers at once. Is this doable in bash? In a perfect world I need to avoid installing applications if at all ...

How do I delete a bash function?

I have done this: bash $ z() { echo 'hello world'; } How do I get rid of it? ...

Detecting failure of a Bash "export" value

In Bash I'm executing a command and putting the result in a variable like this: export var=`svn ls` But if SVN fails for some reason--say it returns a non-zero error code--export still returns status code 0. How do I detect if the executed command fails? ...

Bash script, match on dates like?

I'm writing a script to remove some build artifacts older than 1 week. The files have names in the form artifact-1.1-200810391018.exe. How do I go about removing only the files that are greater than 1 week old, excluding the time in hours and minutes at the end of the date-time-stamp? Currently it is removing all of the files in the ...

How to delete files older than X hours

I'm writing a bash script that needs to delete old files. It's currently implemented using : find $LOCATION -name $REQUIRED_FILES -type f -mtime +1 -delete This will delete of the files older than 1 day. However, what if I need a finer resolution that 1 day, say like 6 hours old? Is there a nice clean way to do it, like there is usi...

Run command when bash script is stopped

How can i, in a bash script, execute a command when the user stops the script (with ctrl - c)? Currently, i have this: afplay file.mp3 while true: do osascript -e "set volume 10" end But i would like it to execute killall afplay when the user is finished with it, regardless if it is command-c or another keypress. ...

rearrange data

If I have a list of data in a text file seperated by a new line, is there a way to append something to the start, then the data, then append something else then the data again? EG a field X would become new X = X; Can you do this with bash or sed or just unix tools like cut? EDIT: I am trying to get "ITEM_SITE_ID :{$row['ITEM_SITE_ID...

chroot + execvp + bash

Update Got it! See my solution (fifth comment) Here is my problen: I have created a small binary called "jail" and in /etc/password I have made it the default shell for a test user. Here is the -- simplified -- source code: #define HOME "/home/user" #define SHELL "/bin/bash" ... if(chdir(HOME) || chroot(HOME)) return -1; ... char *s...

How to iterate over arguments in bash script

I have a complex command that I'd like to make a shell/bash script of. I can write it in terms of $1 easily: foo $1 args -o $1.ext I want to be able to pass multiple input names into the script - what's the right way to do this? Of course I want to handle filenames with spaces in them. ...

How to properly handle wildcard expansion in a bash shell script?

#!/bin/bash hello() { SRC=$1 DEST=$2 for IP in `cat /opt/ankit/configs/machine.configs` ; do echo $SRC | grep '*' > /dev/null if test `echo $?` -eq 0 ; then for STAR in $SRC ; do echo -en "$IP" echo -en "\n\t ARG1=$STAR ARG2=$2\n\n" done else ...

Why do double-quote change the result

I have a simple linux script: #!/bin/sh for i in `ls $1` do echo $i done In my temp folder are 4 file: a.a, a.aa, a.ab and a.ac When i call ./script temp/*.?? i get: temp/a.aa When i call ./script "temp/*.??" i get: temp/a.aa temp/a.ab temp/a.ac Why do the double quote change the result? ...

more efficent shell text manipulation

Hello, I am using this command: cut -d\: -f2 To sort and reedit text, Is there a more efficient way to do this without using sed or awk? I would also like to know how I would append a period to the end of each field At the moment the output is like $x['s'] and I would like it to be $x['s'] . Just using standard unix tools edit: I ...

Prevent 'cp' from outputting the file name from a bash script

I have a shell script which copies a few files to the current directory, compresses them, and streams the compressed file to stdout. On the client side I use plink to execute the script and stream stdin to a file. This almost works. It seems that the cp command outputs the file name being copied when its executed from inside the scrip...

Going backwards in a bash prompt

I'd like to have a blank line after my bash prompt and before the output on my Mac. It should look like this would: echo; ls Can I add a newline to my bash prompt and then go back up one line to wait for user input? Is there something obvious I'm missing? ...

Easy parallelisation

I often find myself writing simple for loops to perform an operation to many files, for example: for i in `find . | grep ".xml$"`; do bzip2 $i; done It seems a bit depressing that on my 4-core machine only one core is getting used.. is there an easy way I can add parallelism to my shell scripting? EDIT: To introduce a bit more contex...