Copy Option


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copy Option
# 1  
Old 04-28-2009
Copy Option

I need to be able to choose an option in order to copy a file. So far, I have this:

Code:
echo "Please input file name after option and use -c to copy, -r
replace, or -x to delete"

if [ $# -eq 0 ]
   then
      echo "You must provide a filename!"
      exit 1
fi

if [ "1$" = "-c" ]
   then
   cp -- "$@"
   echo "Copying Complete."
   exit1
fi

However it's not working Smilie... any ideas?
# 2  
Old 04-28-2009
Quote:
if [ "1$" = "-c" ]
Should be
Code:
if [ "$1" = "-c" ]

# 3  
Old 04-28-2009
@squardius:
you are passing filename as an argument. you are checking the condition

Code:
if [ "$1" = "-c" ]

can you post what you are actually passing?
# 4  
Old 04-28-2009
Thanks methyl for the hint...duh

However, I'm still getting this after running the program.
Code:
Please input file name after option and use -c to copy, -r 
replace, or -x to delete
cp: cannot stat `-c': No such file or directory
Copying Complete.

khare, what I would like to do is to be able to specify an option with a certain file name, for example -c to copy, -x to delete, and -r to replace.
Hope that makes sense.
# 5  
Old 04-29-2009
try something like...


Code:
if [ "$1" = "-c" ]; then
        cp -- $2 $3
 
elif [ "$1" = "-r" ]; then
        mv -- $2 $3
 
elif [ "$1" = "-x" ]; then
        rm -- $2 $3
else
        echo "wrong option"
        exit
fi



usage:

Code:
./options.sh -c file1 file2
./options.sh -r file1 file2
./options.sh -x file1 file2

# 6  
Old 04-30-2009
Thanks Khare,
Here is the whole script:
Code:
if [ "$1" = "-c" ]; then
        cp -- $2 $3
 
elif [ "$1" = "-r" ]; then
        mv -- $2 $3
 
elif [ "$1" = "-x" ]; then
        rm -- $2 $3
else
        echo "wrong option"
        exit
fi

Upon running it with any option above, I'm getting:
Code:
Please input file name after option and use -c to copy, -r 
replace, or -x to delete
option: line 21: syntax error: unexpected end of file

I tried changing "Exit" to Exit 1 but that didn't help!
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. OS X (Apple)

Copy files between partitions with "without auto-mount" option enabled - possible?

Hello, fellow Unixers and Macers, I'll run several OS X versions each on its own dedicated partition. Partitions won't be seeing each other (by editing fstab). Question: will I be able to copy files between those partitions? Partition #1: Mavericks. Partition #2: HighSierra (2 Replies)
Discussion started by: scrutinizerix
2 Replies

2. Solaris

Unrecognized option: sparc-sun-Solaris2.10/bin/as: unrecognized option `-m32'

Hi, I installed some packages required by an app built with python. But when I try python setup.py install, I get the following error: /opt/csw/lib/gcc/sparc-sun-solaris2.10/5.2.0/../../../../sparc-sun-solaris2.10/bin/as: unrecognized option `-m32' Could anyone tell me what's wrong... (4 Replies)
Discussion started by: Kimkun
4 Replies

3. Shell Programming and Scripting

how to copy the directory but not copy certain file

Hi experts cp bin root src /mnt but not copy bin/bigfile any help? ( I post this thread in the "redhat" forum wrongly, I don't know how to withdraw that question in that wrong forum) Thanks (6 Replies)
Discussion started by: yanglei_fage
6 Replies

4. Shell Programming and Scripting

recently introduced to the newer option for find...does an older option exist?

To find all the files in your home directory that have been edited in some way since the last tar file, use this command: find . -newer backup.tar.gz Is anyone familiar with an older solution? looking to identify files older then 15mins across several directories. thanks, manny (2 Replies)
Discussion started by: mr_manny
2 Replies

5. UNIX for Dummies Questions & Answers

What is in-core copy and disk-copy of i-node table?

I have found a question from the exercises of my study mat. The question is "Why are there a in-core copy and a disk-copy of i-node block and super block?" If any one know the proper answer then please send me..... (1 Reply)
Discussion started by: dearanik
1 Replies

6. UNIX for Advanced & Expert Users

Problem with Carbon copy (CC) option in mailx command

Hi, I have problems with the cc option in mailx command. Just went through some of the similar threads but none provides a satisfactory explanation. I have a script using the mailx command in the following way: (echo `cat mailsub.txt` ; uuencode attachment.csv attachment.csv) | mailx -s... (2 Replies)
Discussion started by: SmithaN
2 Replies

7. Shell Programming and Scripting

option followed by : taking next option if argument missing with getopts

Hi all, I am parsing command line options using getopts. The problem is that mandatory argument options following ":" is taking next option as argument if it is not followed by any argument. Below is the script: while getopts :hd:t:s:l:p:f: opt do case "$opt" in -h|-\?)... (2 Replies)
Discussion started by: gurukottur
2 Replies

8. Shell Programming and Scripting

Copy Files then with option of remove

hi, i encounter a quite a new things for me. lets sat my test.txt modified date is 6th of July 2005. then i execute the following command: "cp test.txt testfolder/test.txt" when i go to execute "ls -l" command... the test.txt modified date become today date! but if i execute: ... (5 Replies)
Discussion started by: maldini
5 Replies
Login or Register to Ask a Question