1
# define some variables for later date=`date` usr=`whoami` # define usage function to echo syntax if no args given usage(){ echo "error: filename not specified" echo "Usage: $0 filename directory/ directory/ directory/" exit 1 } # define copyall function copyall() { # local variable to take the first argument as file local file="$1" dir # shift to the next argument(s) shift # loop through the next argument(s) and copy $file to them for dir in "$@"; do cp -R "$file" "$dir" done } # function to check if filename exists # $f -> store argument passed to the script file_exists(){ local f="$1" [[ -f "$f" ]] && return 0 || return 1 } # call usage() function to print out syntax [[ $# -eq 0 ]] && usage 

here's what I can't figure out

# call file_exists() and copyall() to do the dirty work if ( file_exists "$1" ) then copyall 

I would also love to figure out how to take this next echo section and condense it to one line. Instead of $1 then shift then move on. Maybe split it into an array?

 echo "copyall: File $1 was copied to" shift echo "$@ on $date by $usr" else echo "Filename not found" fi exit 0 
1
  • You can drop the && return 0 || return 1; having the last line of the function simply be [[ -f "$f" ]] will cause 0 to be returned if the file exists and 1 if it doesn't. Commented Apr 13, 2013 at 2:55

2 Answers 2

1

It seems to me that the file_exists macro is superfluous:

if [ -f "$1" ] then copy_all "$@" else echo "$0: no such file as $1" >&2; exit 1 fi 

or even just:

[ -f "$1" ] && copy_all "$@" 
Sign up to request clarification or add additional context in comments.

Comments

0

You probably just need to remove the parentheses around file_exists "$1", and add a semicolon:

if file_exists "$1"; then copyall 

3 Comments

just fixed the formatting; perhaps it will be easier to read now. I also tried removing the ()'s and still no go.
it just echos file was copied but the copyall function isn't even called it seems. no errors or anything. even with an echo in the copyall function, that doesn't even display.
needed to be copyall "$@" and that fixed it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.