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. Sincerely, Piet |
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.
Code:
(1) find "/usr/local /cpanel/base/" -type f -name "favicon.ico" -print http://tldp.org/LDP/Bash-Beginners-G...ners-Guide.pdf http://tldp.org/LDP/abs/abs-guide.pdf |
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. Sincerely, Piet |
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" . |
Hey Piet, you're not running a script, but just commands. Create a script and then add these commands in that, then invoke it, as:
Code:
#!/bin/bash |
Hi shivaa,
I tried, but get a error! FILE: Code:
#!/bin/bash Code:
# sh favicon_ico.sh |
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 $? - 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 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' 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 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. 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. http://mywiki.wooledge.org/BashFAQ/031 http://mywiki.wooledge.org/ArithmeticExpression 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. Code:
workpath='/usr/local/cpanel/base' 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. Scripting With Style |
Quote:
Code:
~$ vi favicon_ico.sh Code:
~$ bash -xv favicon_ico.sh |
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.
http://www.linuxquestions.org/questi...03#post4962903 |
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 Code:
1st. run Can there be something wrong with the setup in my server, I have VPS at HG using RedHat 9.3.6 Regards, Piet |
What is the outcome if you type?
Quote:
Quote:
|
Hi rtmistler,
Code:
# grep SHELL |
1.
Code:
-bash: ./favicon_ico.sh: /bin/bash^M: bad interpreter: No such file or directory 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 Code:
chmod u+x test.sh Code:
./test.sh 5. re Redhat 9.3.6: no such OS, so show output of Code:
uname -a 6. You must bookmark and read these http://rute.2038bug.com/index.html.gz http://tldp.org/LDP/Bash-Beginners-G...tml/index.html http://www.tldp.org/LDP/abs/html/ |
Hello Chris,
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. Patience is a virtue) Sincerely, Piet |
All times are GMT -5. The time now is 07:39 AM. |