If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Help with shell script!

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-30-04, 10:26
zeta_acosta zeta_acosta is offline
Registered User
 
Join Date: Mar 2004
Posts: 8
Help with shell script!

Hi all, I'm struggling to grasp this code and could do with it being explained to me! Can anyone help? Thanks so much!

#!/bin/sh

path() {
case "$1" in
*/*)
echo "$1"
;;
*)
echo "$HOME/$1"
;;
esac
}

echo -n 'Source: '
read src
src=`path $src`

if [ ! -e "$src" ]; then
echo $src does not exist >&2
exit 1
fi

echo -n 'Destination: '
read dst
dst=`path $dst`

mv $src $dst
Reply With Quote
  #2 (permalink)  
Old 03-30-04, 11:01
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
Re: Help with shell script!

#!/bin/sh use this shell

path() { define function 'path'

case "$1" in
*/*) if 1st param supplied to function contains '/'
echo "$1"output the value of $1 to stdout

;;
*) otherwise, prepend the value of $HOME and output the new value to stdout
echo "$HOME/$1"
;;
esac
}

echo -n 'Source: ' Output 'Source:' to stdout (suppress carriage return)
read src read a line from stdin and assign this value to the variable 'src'

src=`path $src` pass the value of 'src' to the 'path' function and overwrite the value of 'src' with the output from 'path'

if [ ! -e "$src" ]; then test if the value of 'src' exists as a file
echo $src does not exist >&2 if no file exists, send a message to stderr
exit 1 quit the script, set $? = 0
fi

echo -n 'Destination: ' Output 'Destination:' to stdout (suppress carriage return)
read dst read a line from stdin and assign this value to the variable 'dst'

dst=`path $dst`pass the value of 'dst' to the 'path' function and overwrite the value of 'dst' with the output from 'path'


mv $src $dst move the file to the dst location

Last edited by Damian Ibbotson; 03-30-04 at 11:04.
Reply With Quote
  #3 (permalink)  
Old 03-30-04, 13:46
zeta_acosta zeta_acosta is offline
Registered User
 
Join Date: Mar 2004
Posts: 8
Thumbs up

Thanks for that, you've been a great help!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On