bash

how do you normalize a file path in bash?

I want to transform "/foo/bar/.." to "/foo" Is there a bash command which does this? ...

Bash Shell Scripting: what simple logic am I missing

This may be too generic a question as is but... I am stumped by trying to move through the directories from within a shell script. I'm not a *nix power user, but I am comfortable working through the command line for most tasks. I'd like to call a script that can move 'me' to a directory instead of just the script process similar to the f...

How can I automatically quote or group commandline arguments for alias in bash?

I have a script that takes a command and executes it on a remote host. It works fine, for example: $ rexec "ant build_all" will execute the "ant build_all" command on the remote system (passing it through SSH, etc). Because I'm lazy, I want to set up an alias for this command (and ultimately, several others), such that, for example,...

Connecting from Linux to Windows to perform a task

I've been asked to find a way to connect from a Linux system to one of several Windows servers. What we need to do ideally is connect to whatever Windows server is causing the trouble, kill a process, and restart the process. Ideally, it would be something that could be put into a script that could be run from the Linux computer. All ...

How Do I Copy Up To An SSH Host All Folders and Files Except Certain Ones?

Using the Linux command line, I use the scp command to copy up all the files and folders from a certain directory. However, I don't like to consume wasted bandwidth for copying up things I rarely change like my tiny_mce folder. What's the trick to copy up everything but skip a short list of folders? ...

Error Trying To Make Easier rsync Command

I normally use scp to copy stuff, but now I'm trying to get used to the more powerful rsync command. It helps me use less bandwidth by copying up only files that have changed. However, rsync has a lot of complex parameters, so I thought, hey, I'll just make a little Bash script that makes it easy for me, and call the command 'rscp'. So, ...

How do I add a directory with a colon to PYTHONPATH?

The problem is simple: Using bash, I want to add a directory to my PYTHONPATH for ease of script execution. Unfortunately, the directory I want to use has a : in it. So I try each of the following export PYTHONPATH=${PYTHONPATH}:/home/shane/mywebsite.com:3344/ export PYTHONPATH=${PYTHONPATH}:/home/shane/mywebsite.com\:3344/ export PYTH...

URLEncode from a bash script

I am trying to write a bash script for testing that takes a parameter and sends it through curl to web site. I need to url encode the value to make sure that special characters are processed properly. What is the best way to do this? Here is my basic script so far: #!/bin/bash host=${1:?'bad host'} value=$2 shift shift curl -v -d "...

how to escape white space in bash loop list

I have a bash shell script that loops through all child directories (but not files) of a certain directory. The problem is that some of the directory names contain spaces. Here are the contents of my test directory: $ls -F test Baltimore/ Cherry Hill/ Edison/ New York City/ Philadelphia/ cities.txt And the code that loops thr...

AWK: redirecting script output from script to another file with dynamic name

Hi all, I know I can redirect awk's print output to another file from within a script, like this: awk '{print $0 >> "anotherfile" }' 2procfile (I know that's dummy example, but it's just an example...) But what I need is to redirect output to another file, which has a dynamic name like this awk -v MYVAR"somedinamicdata" '{print $0 ...

How do I use regular expressions in bash scripts?

I want to check if a variable has a valid year using a regular expression. Reading the bash manual I understand I could use the operator =~ Looking at the example below, I would expect to see "not OK" but I see "OK". What am I doing wrong? i="test" if [ $i=~"200[78]" ] then echo "OK" else echo "not OK" fi ...

How to I export a system scope environment variable in bash

I need to set a system environment variable from a bash script that would be available outside of the current scope. So you would normally export environment variables like this: export MY_VAR=/opt/my_var But I need the environment variable to be available at a system level though. Is this possible? ...

Moving a directory atomically

I have two directories in the same parent directory. Call the parent directory base and the children directories alpha and bravo. I want to replace alpha with bravo. The simplest method is: rm -rf alpha mv bravo alpha The mv command is atomic, but the rm -rf is not. Is there a simple way in bash to atomically replace alpha with br...

What's the best way to check that environment variables are set in Unix shellscript

I've got a few Unix shell scripts where I need to check that certain environment variables are set before I start doing stuff, so I do this sort of thing: if [ -z "$STATE" ]; then echo "Need to set STATE" exit 1 fi if [ -z "$DEST" ]; then echo "Need to set DEST" exit 1 fi which is a lot of typing. Is there a more el...

How do I replace 'php3' with 'php' inside every file in a directory.

Hopefully a nice simple one. I've got a php3 website that I want to run on php 5.2 To start with I'd just like to have every reference to the current "index.php3" _within_each_file_ (recursively) changed to "index.php" and then move on to worrying about globals etc. K. Go! :) Update: Thanks a lot! I realise that my question missed ...

How to delete files older than N weeks from a Microsoft FTP server

I run an OpenSuse server that uploads zipped source code backups to a Microsoft FTP server every night. I have written a Bash script that does this through a cron job. I want to delete backed up files that are older than a certain date. How could I do this? Thanks. ...

How can I append the name of a file to end of each line in that file?

I need to do the following for hundreds of files: Append the name of the file (which may contain spaces) to the end of each line in the file. It seems to me there should be some way to do this: sed -e 's/$/FILENAME/' * where FILENAME represents the name of the current file. Is there a sed variable representing the current filename? ...

Bash configuration on Mac OS X 10.4+

When configuring the bash on OSX via ~/.profile it seems to completely ignore it as soon as ~/.bash_profile exists. If this is correct and expected behavior, should I add my extra configuration stuff to ~/.bash_profile as well or use it instead of ~/.profile? Thanks in advance. ...

are there any commands that are usually used for comparing time in bash shell script?

this was for an old computer class project which I am over with and now am wondering if there was a better way of doing things . we used to program an appointment.sh program where I solved the comparing time using an algorithm which converts time to a certain number you can compare today and tomorrow and of course what happens is it c...

parse csv file using gawk

How do you parse a csv file using gawk? Simply setting FS="," is not enough, as a quoted field with a comma inside will be treated as multiple fields. Example using FS="," which does not work: file contents: one,two,"three, four",five "six, seven",eight,"nine" gawk script: BEGIN { FS="," } { for (i=1; i<=NF; i++) printf "field #...