LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 05-31-2013, 08:13 AM   #1
pietmomberg
LQ Newbie
 
Registered: May 2013
Location: Denmark
Posts: 8

Rep: Reputation: Disabled
Cool 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
 
Old 05-31-2013, 08:26 AM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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

Last edited by shivaa; 05-31-2013 at 08:30 AM.
 
2 members found this post helpful.
Old 05-31-2013, 08:41 AM   #3
pietmomberg
LQ Newbie
 
Registered: May 2013
Location: Denmark
Posts: 8

Original Poster
Rep: Reputation: Disabled
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
 
Old 05-31-2013, 09:16 AM   #4
bloodstreetboy
Member
 
Registered: May 2012
Posts: 201
Blog Entries: 3

Rep: Reputation: 37
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" .
 
Old 05-31-2013, 09:16 AM   #5
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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
 
1 members found this post helpful.
Old 05-31-2013, 10:08 AM   #6
pietmomberg
LQ Newbie
 
Registered: May 2013
Location: Denmark
Posts: 8

Original Poster
Rep: Reputation: Disabled
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'
#
 
Old 05-31-2013, 10:34 AM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,877
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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.
 
2 members found this post helpful.
Old 05-31-2013, 10:37 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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
 
2 members found this post helpful.
Old 05-31-2013, 10:44 AM   #9
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Quote:
Originally Posted by pietmomberg View Post
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

Last edited by shivaa; 05-31-2013 at 10:45 AM.
 
1 members found this post helpful.
Old 05-31-2013, 11:44 AM   #10
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,877
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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
 
Old 05-31-2013, 02:56 PM   #11
pietmomberg
LQ Newbie
 
Registered: May 2013
Location: Denmark
Posts: 8

Original Poster
Rep: Reputation: Disabled
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.
 
Old 05-31-2013, 03:10 PM   #12
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,877
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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

Last edited by rtmistler; 05-31-2013 at 03:11 PM.
 
Old 05-31-2013, 03:46 PM   #13
pietmomberg
LQ Newbie
 
Registered: May 2013
Location: Denmark
Posts: 8

Original Poster
Rep: Reputation: Disabled
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*
 
Old 05-31-2013, 07:15 PM   #14
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,348

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
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/

Last edited by chrism01; 06-02-2013 at 10:46 PM.
 
1 members found this post helpful.
Old 06-01-2013, 05:01 AM   #15
pietmomberg
LQ Newbie
 
Registered: May 2013
Location: Denmark
Posts: 8

Original Poster
Rep: Reputation: Disabled
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
copy file and rename n times in succession coryjsanders Linux - Newbie 2 12-08-2012 12:06 AM
Shell script to get name of file, delete original file, rename blank file chrisgti Linux - General 11 09-15-2012 02:49 AM
Get first file of directory then copy to other directory andd rename the file Faye Linux - Software 4 01-30-2011 09:16 AM
can't find zm script file to copy. Buddhike G Linux - Software 1 10-08-2007 05:17 PM
how to rename a file and copy a file in a shell zach014 Linux - Newbie 6 11-23-2006 09:23 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration