CP question - Grab file name from first argument and rename that to the same filename.bak
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Please show complete example, ie. what is the value of $1?
So for instance I'm using a filename with a space in it.
bash bakctrol help me
bakctrol being my script which I believe to have working, possibly. with the one caveat that it does not play nicely with spaced filenames, which is something I would like to accomplish if run regularly.
Output of bakctrol
#!/bin/bash
#This will copy the filename called in the argument and append a .bak to it, will be used for version control in this environment.
cp "$1" "${1}".bak
You must quote a file name with spaces also when you are calling your script
Code:
bash bakctrol "help me"
If you add a loop
Code:
#!/bin/bash
#This will copy the filename called in the argument and append a .bak to it, will be used for version control in this environment.
#loop over all arguments
for arg
do
cp "$arg" "$arg".bak
done
Then you can run it with several arguments (file names)
Code:
./bakctrol "help me" file2 file3
Last edited by MadeInGermany; 10-27-2016 at 05:00 AM.
Reason: (file names)
Actually, that's normal/std for just about every *nix cmd: spaces are treated as param separators unless specially dealt with, as you've discovered ...
Basically, do not create filenames with spaces, AND if any are given to you to process, I always rename (or take a copy) with underscores instead of spaces and then (if reqd) put spaces back at the end.
Trust me, you'll thank me later ...
Actually, that's normal/std for just about every *nix cmd: spaces are treated as param separators unless specially dealt with, as you've discovered ...
Basically, do not create filenames with spaces, AND if any are given to you to process, I always rename (or take a copy) with underscores instead of spaces and then (if reqd) put spaces back at the end.
Trust me, you'll thank me later ...
He is right ya know. Spaces can be a bitch, and them specail characters. Don't get this guy talking about them.
Trust me, you'll thank me later ...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.