Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2004
    Posts
    8

    Unanswered: 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

  2. #2
    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 12:04.

  3. #3
    Join Date
    Mar 2004
    Posts
    8

    Thumbs up

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •