I'm writing an alias that takes input for a file name and then after making sure the file is regular and readable copies the file using cp -i to the back up folder $HOME/Backup

I am very new to UNIX so I am experiencing some difficulties accomplishing this. Here is my code:

alias getname='read filename'

alias vfile='getname; if [ ! -f $filename ]; then echo "Irregular file"; (exit 1); elif [ ! -r $file ]; then echo "Not readable"; (exit 2); fi;'

alias backup='vfile; if [ vfile ]; then cp -i $filename $home/Backup; fi;'

I have tested the vfile alias and it works, the errors I am getting are:

-bash: syntax error near unexpected token ';'

I started getting this error as soon as I included vfile as the first operation; if I don't run vfile before the if statement, it will use the filename from the last time it ran. I have to make sure that vfile did not produce an error before I can copy it.

Before I added vfile as the first command I got this error:

cp: cannot create regular file '/Backup/*': No such file or directory

but there is indeed a directory named Backup in my home folder so I don't know what's causing this either.

  • Is your instructor asking you to make these things aliases, or is that something you've come up with? If you have flexibility, you might instead consider writing functions. That'll enable you to more easily write multi-line expressions. – Andy Dalton Oct 13 '17 at 1:57

Your alias vfile ends with a ;, and then the alias backup starts off with vfile;. So that's two ;s in succession without a statement in between, which is not allowed:

$ echo ; ;
bash: syntax error near unexpected token `;'

Eliminate the ; at the end of vfile.


Use my alias from your last question instead of having the two aliases.

Since your vfile alias will throw an error code if wrong you can do this:

alias filebackup='vfile && cp -i "$filename" "$HOME/Backup/"'

The && will tell it only to execute the cp command if vfile exits with 0

EDIT sorry I'm tired

$home is not a valid environmental variable so unless you set it manually somewhere that explains why you got the no such file or directory error because it expanded to ''. Also always quote your variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.