LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   HowTo: Find a file, rename and copy new file in one script! (https://www.linuxquestions.org/questions/linux-newbie-8/howto-find-a-file-rename-and-copy-new-file-in-one-script-4175464223/)

pietmomberg 05-31-2013 08:13 AM

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

shivaa 05-31-2013 08:26 AM

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

(2) mv favicon.ico <new_file_name>

(3) cp -p "/home/domain/public_html/favicon.ico" .

Let's know if you stuck somewhere. Also go through following guides:
http://tldp.org/LDP/Bash-Beginners-G...ners-Guide.pdf
http://tldp.org/LDP/abs/abs-guide.pdf

pietmomberg 05-31-2013 08:41 AM

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

bloodstreetboy 05-31-2013 09:16 AM

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" .

shivaa 05-31-2013 09:16 AM

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
if [ -f /usr/local/cpanel/base/favicon.ico ]; then
mv /usr/local/cpanel/base/favicon.ico /usr/local/cpanel/base/<new_file_name>
echo "File rename done."
cp /usr/local/cpanel/base/<new_file_name> .
echo "File copy done."
else
echo "File not found."
fi


pietmomberg 05-31-2013 10:08 AM

Hi shivaa,

I tried, but get a error!

FILE:
Code:

#!/bin/bash
if [ -f /usr/local/cpanel/base/favicon.ico ]; then
mv /usr/local/cpanel/base/favicon.ico /usr/local/cpanel/base/favicon_org.ico
echo "File rename done."
cp /usr/favicon.ico /usr/local/cpanel/base/favicon.ico
echo "File copy done."
else
echo "File not found."
fi

OUTPUT:
Code:

# sh favicon_ico.sh
favicon_ico.sh: line 9: syntax error near unexpected token `fi'
favicon_ico.sh: line 9: `fi'
#


rtmistler 05-31-2013 10:34 AM

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.

David the H. 05-31-2013 10:37 AM

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.

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'

mv "$workpath/favicon.ico" "$workpath/favicon_org.ico"

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.

Scripting With Style

shivaa 05-31-2013 10:44 AM

Quote:

Originally Posted by pietmomberg (Post 4962842)
I tried, but get a error!

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:
Code:

~$ vi favicon_ico.sh
~$ chmod a+x favicon_ico.sh
~$ ./favicon_ico.sh
#Or
~$ bash favicon_ico.sh

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:
Code:

~$ bash -xv favicon_ico.sh

rtmistler 05-31-2013 11:44 AM

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

pietmomberg 05-31-2013 02:56 PM

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

rtmistler 05-31-2013 03:10 PM

What is the outcome if you type?

Quote:

env | grep SHELL
Also what is the outcome if you check bash and sh?

Quote:

ls -l /bin/bash
ls -l /bin/sh

pietmomberg 05-31-2013 03:46 PM

Hi rtmistler,

Code:

# grep SHELL
SHELL=/bin/bash

# ls -l /bin/bash
-rwxr-xr-x 1 root root 768664 Jul 22  2011 /bin/bash*

# ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Jul 22  2011 /bin/sh -> bash*


chrism01 05-31-2013 07:15 PM

1.
Code:

-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

5. re Redhat 9.3.6: no such OS, so show output of
Code:

uname -a

cat /etc/*release*


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/

pietmomberg 06-01-2013 05:01 AM

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.