LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 03-21-2012, 03:15 PM   #1
rit
LQ Newbie
 
Registered: Feb 2012
Location: kanpur,india
Posts: 21

Rep: Reputation: Disabled
having problem in cp $1 $filename


hi everyone

Code:
#!/bin/sh
if test "$1" = "" ; then
        echo "Usage: backup-lots.sh <filename>"
        exit
fi
for i in 0 1 2 3 4 5 6 7 8 9 ; do
        NEW_FILE=$1.BAK-$i
        if test -e $NEW_FILE ; then
                echo "backup-lots.sh: **warning** $NEW_FILE"
                echo "                already exists - skipping"
        else
                cp $1 $NEW_FILE
        fi
done
i am reading a ebook on shell scripts and i found this script.
i didn't understood the last else part which is cp $1 $NEW_FILE.
how the first command argument will be copied in NEW_FILE.

Last edited by Tinkster; 03-21-2012 at 03:36 PM. Reason: code tags added
 
Old 03-21-2012, 03:35 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
What exactly is there not to be understood?

$1 is the argument you passed on the command-line.
$NEW_FILE is generated for each iteration of your
for loop.
cp copies the file by the first name to the file by newname
if the target doesn't exist yet.


Cheers,
Tink

P.S.: Please use code tags for your snippets, makes them
easier to read.
 
1 members found this post helpful.
Old 03-21-2012, 03:51 PM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
The script is creating 10 backup copies of the input filename

$1 is a variable that contains the first command line argument to the script (the name of the file you want to backup)
$NEW_FILE is a variable that is declared to be $1.BAK-$i, where $i loops from 0 to 9

The script tests if $NEW_FILE exists, if so it skips it, if not it copies $1 to $NEW_FILE
 
1 members found this post helpful.
Old 03-21-2012, 03:54 PM   #4
rit
LQ Newbie
 
Registered: Feb 2012
Location: kanpur,india
Posts: 21

Original Poster
Rep: Reputation: Disabled
thankyou for the help
the ebook i am referring from last 2 days has only scripts,nothing is explained in there like what does $1 do and ......
please suggest me a detailed ebook for shell script.
 
Old 03-21-2012, 03:59 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
I don't know many e-Books ... how about HTML resources? You could always install calibre and
try to convert HTML to epub/kindle format. ;}


Cheers,
Tink
 
Old 03-21-2012, 04:09 PM   #6
rit
LQ Newbie
 
Registered: Feb 2012
Location: kanpur,india
Posts: 21

Original Poster
Rep: Reputation: Disabled
thanyou
please suggest me a detailed ebook on shell script
or html resources
 
Old 03-21-2012, 04:24 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
 
Old 03-21-2012, 04:45 PM   #8
rit
LQ Newbie
 
Registered: Feb 2012
Location: kanpur,india
Posts: 21

Original Poster
Rep: Reputation: Disabled
thankyou
i liked first one linux shell scripting tutorial.
i will get started with it.
 
Old 03-21-2012, 07:07 PM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
A general hint; try adding
Code:
set -xv
at the top of the prog (after the #! line) ; it shows you exactly what's going on.
 
Old 03-22-2012, 11:21 AM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
As mentioned, please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.


Now for a few comments on the script in the OP. There are a couple of poorly-written points about it.


1) First, and foremost, QUOTE ALL OF YOUR VARIABLE SUBSTITUTIONS. You should never leave the quotes off a variable expansion unless you explicitly want the resulting string to be word-split by the shell. This is a vitally important concept in scripting, so train yourself to do it correctly now. You can learn about the exceptions later.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes


2) Environment variables are generally all upper-case. So while not absolutely necessary, it's good practice to keep your own user variables in lower-case or mixed-case, to help differentiate them.


3) #!/bin/sh is used for interpreting scripts in posix-compliant lowest-common-denominator mode. This is mostly recommended for system startup scripts and other cases where portability and standardization are more important than convenience. When this isn't required, you should probably use #!/bin/bash or another shell that has more modern, advanced features available, such as ksh or zsh.

The final two points assume that your shell is bash or ksh.


4) It's recommended to use ((..)) for numerical tests, and [[..]] for string tests and other complex expressions. Don't use the old [..]/test unless you specifically need POSIX-style portability.

http://mywiki.wooledge.org/ArithmeticExpression
http://mywiki.wooledge.org/BashFAQ/031
http://wiki.bash-hackers.org/commands/classictest
http://wiki.bash-hackers.org/syntax/...nal_expression


5) brace expansion can be used to generate a sequence of numbers. You can also use a c-style for loop.


Here are a few useful bash scripting references:
http://mywiki.wooledge.org/BashGuide
http://mywiki.wooledge.org/BashFAQ
http://mywiki.wooledge.org/BashPitfalls
http://www.linuxcommand.org/index.php
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/index.html
http://www.gnu.org/software/bash/manual/bashref.html
http://wiki.bash-hackers.org/start
http://ss64.com/bash/

I particularly recommend the first one for general concepts, and the two following it for overcoming problems with your code.
 
Old 03-22-2012, 11:36 AM   #11
rit
LQ Newbie
 
Registered: Feb 2012
Location: kanpur,india
Posts: 21

Original Poster
Rep: Reputation: Disabled
thankyou david
 
  


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
Change name of backup file in ext3 from filename~ to .filename~ Libu Linux - General 2 07-21-2008 10:29 PM
Convert static library (Filename.a) to dynamic shared object (filename.so) afx2029 Linux - Software 4 08-17-2007 07:07 AM
Odd filename problem.. awpti Linux - Software 6 04-29-2007 12:05 AM
is my filename cause my problem? veilig Programming 2 04-22-2006 11:32 AM
change uploaded files from filename.avi to filename.avi.html like www.rapidshare.de latheesan Linux - Newbie 3 06-16-2005 05:33 AM


All times are GMT -5. The time now is 02:32 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration