[SOLVED] HowTo: Find a file, rename and copy new file in one script!
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.
HowTo: Find a file, rename and copy new file in one script!
Hello,
Is there anyone who will help me to make a script that can do this.
1) Find a file "favicon.ico" in the directory "/usr/local /cpanel/base/" incl. sub
2) Rename file to "favicon_org.ico" in the current directory
3) Copy this file "/home/domain/public_html/favicon.ico" to the current directory
And finally, the script should be run every 4 hours as cron.
What you've done so far? Please do not expect from anyone to give you a ready made script. Well, just for your reference, following commands will help you.
Hi shivaa,
it's not because I'm trying to get a go an run solution, the more info the better it is.
I have tried in so many ways now that I just can not find head or tail of what I've done :-/. I tried with your code and got this output.
#sh favicon_ico.sh
'ind: invalid predicate `-print
mv: cannot stat `favicon.ico': No such file or directory
cp: missing destination file operand after `/home/lds/public_html/favicon.ico'
Try `cp --help' for more information.
Have you put the right path of the file favicon.ico?
Have you see and use the dot (.) which shivaa put after "/home/domain/public_html/favicon.ico"?
"/home/domain/public_html/favicon.ico" .
That script works. Shivva is making the point that you need to write "your" script first and give it exactly as shivva has done, for people to offer suggestions as to what you've done incorrectly.
Here are script suggestions:
1.) You can type your entire script, although shorter is better, onto the command line. I say shorter because it's just easier.
2.) You can turn on some debugging using "set -xv"
Here's an example of performing ls via the command line and then via a script, and what you see with the -xv setting:
Command Line
Code:
desktop:~/testcode$ set -xv; ls; echo $?
+ ls --color=auto
timestamps.c
+ echo 0
0
desktop:~/testcode$
Explanation:
- I typed "set -xv; ls; echo $?"
- The semicolon delimits between each statement, sort of a line break.
- The set -xv turns on more advanced debugging so when you see a line with the plus "+" in front, that's a repeat of the command it was told to perform.
- My particular ls is alias to be "ls --color=auto", hence why it shows that way
- This directory contains one file, timestamps.c that's why you see that
- My next command was to echo out the result of the ls command. "echo $?" means to echo "$?" and $? in script language means the last result. Since there were no arguments to ls that were problems, or I didn't try to ls a file name which doesn't exist, like "ls foobar.nothing", the ls command succeeded and returned 0.
So you can see the debug helps you to diagnose if you made errors on any given step. The script for this would be written almost as simply as the in-line commands shown above:
As A Script
Code:
#!/bin/sh
set -xv
ls
echo $?
Comments in the script are pre-pended with #; however the #!/bin/sh is a special designator to indicate that the shell I'm using in this script is the /bin/sh one on my system. Another option might be /bin/bash; however on my system they are the same thing and you'll find more typically people use /bin/sh.
You can leave in the set -xv line by commenting it out once you've debugged the script #set -xv.
Everyone's point here is you can rename and copy a file from the command line. Do so, and then place those commands into a shell script, debug it either using my suggestions or other suggestions, and if you try it a bit and don't succeed, then it is helpful to post that script saying "I tried this, the intent was to do such-and-such", and "this is what happened". If you just say, "I need help running a script to do <blah-blah>" or make it worse and use an exclamation, you'll get suggestions to get you started, or in the case like this, likely a direct answer. This is an easy script, if you cannot work a bit and get it done with the suggestions and pointers, then you're going to have a very difficult time writing more advanced scripts or programs.
When debugging problems, start by actually reading the error message.
Code:
line 9: syntax error near unexpected token `fi'
There's a syntax error somewhere, and the shell found the problem at line 9. Note though that the line number given is the point where the problem became apparent, which may not be the actual place where the error is. So backtrack up from that line and look carefully at the code you wrote for errors.
Frankly though, I can't find any actual syntax errors in the code you posted. It's a mystery to me. Is there any difference between it and what you actually ran?
One thing that may be a factor is the way you're executing it:
Code:
# sh favicon_ico.sh
Don't run your script this way. This pattern first runs an instance of the shell /bin/sh, and uses that to execute your script. But /bin/sh is the system's posix-compliant shell, and may not be capable of handling fancier code features like those of bash.
Since your script has a #!/bin/bash shebang line in it, just chmod it to be executable, and run it directly with a path prefix (or from a directory in your path). It will run with bash as the interpreter, as expected.
Code:
./favicon_ico.sh #if running it from the current directory.
I'm also a bit worried about the "#" in your posted message. As a shell prompt it traditionally means that you're working as the root user. This is something you should avoid doing, unless you are doing something that specifically needs root access. And even then keep it to a minimum.
When using advanced shells like bash or ksh, it's recommended to use [[..]] for string/file tests, and ((..)) for numerical tests. Avoid using the old [..] test unless you specifically need POSIX-style portability.
Move your long, fixed strings, such as file paths, into variables first. These should generally be configured at the top of the script so they can be easily changed when needed. That's the kind of thing they're there for. Good practice says you should keep the code and data as separate as possible.
Be sure to double-quote any string that uses them.
The -r test is generally more useful than the -f test. You want to know if there's a readable file there.
Finally, take some time to format your script so that it's easier to read and debug. Indent your sub-commands and add some blank lines to separate logical sections. Add comments to explain what's going on. When your code is easy to read it's easier to avoid errors and easier to debug them when they occur.
Though David has explained it very well. However, in simple words, this is a bash script, so don't run it with sh scriptname.sh. If your working shell is bash, then simply create your script file, make it executable and then invoke it as:
Moreover, as rtmistler said above, if you're getting any error(s) while executing your script, then invoke it with debug mode, so you can find that where it's going wrong, as:
Happened to be browsing and noticed a parallel example. The originator tried a script, it was horrific, they got some suggestions, the main suggestion was to write it in steps and more cleanly because it was a complicated script. That thread originator did the work to get it done and posted their much cleaner result.
Hi all,
Thank you for your great contribution in helping me get this to work.
Unfortunately, I did not come very far, I keep getting syntakst failures, even in the simple things like this code.
Code:
#!/bin/bash
set -xv
ls
echo $?
Code:
1st. run
# ./favicon_ico.sh
-bash: ./favicon_ico.sh: /bin/bash^M: bad interpreter: No such file or directory
2st. run
# sh favicon_ico.sh
: invalid optionline 2: set: -
set: usage: set [--abefhkmnptuvxBCHP] [-o option] [arg ...]
Can there be something wrong with the setup in my server, I have VPS at HG using RedHat 9.3.6
Regards, Piet
Last edited by pietmomberg; 05-31-2013 at 02:59 PM.
-bash: ./favicon_ico.sh: /bin/bash^M: bad interpreter: No such file or directory
The ^M hints that you're using (or have used) an MSWin (or MAC) editor to create the file.
Don't do that, because Linux and MS have different line endings.
Simplest is to use a Linux editor.
(You could run dos2unix over that file which would convert the line endings, but you're much better off learning to use Linux properly)
2. as above, STOP using the 'sh script' method.
3. Let's start by getting a basic script working , so login to Linux, use an editor like vim and create script test.sh
Code:
#!/bin/bash
set -xv
ls
echo $?
then add execute perm
Code:
chmod u+x test.sh
then run thus
Code:
./test.sh
Post the exact code you wrote, the cmd invocation and the exact output
Thanks for the help, this must be call a NUBE error, (I'm the NUBE) ;-)
Now I will try to make the script as this thread started with, hope all of you who have contributed your great help to me, have the patience to help me if it should be necessary. Thanks in advance to all of you.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.