LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 02-25-2009, 07:52 AM   #31
topheraholic
Member
 
Registered: Aug 2008
Location: shanghai
Distribution: ubuntu
Posts: 128

Original Poster
Rep: Reputation: 15

Quote:
Originally Posted by David the H. View Post
Not exactly. Stubbing means inserting a quick, neutral line as a temporary placeholder, with the intent of coming back later and replacing it with something more useful. That way you can build the basic structure of the script before concentrating on the details of the individual commands. A real stub would be something like:

echo "Replace this line with the mv command".


The suggestion I gave you is more of a debugging test. It helps you see clearly what's happening so you can catch possible mistakes before you use the script on anything important. Sticking in echo lines for variables and commands is a good way to see what your code is really doing.
oh thanks,yeah it is, i get it ,thanks.
sorry for my belated reply for my work.
 
Old 02-26-2009, 09:57 PM   #32
topheraholic
Member
 
Registered: Aug 2008
Location: shanghai
Distribution: ubuntu
Posts: 128

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by David the H. View Post
Not exactly. Stubbing means inserting a quick, neutral line as a temporary placeholder, with the intent of coming back later and replacing it with something more useful. That way you can build the basic structure of the script before concentrating on the details of the individual commands. A real stub would be something like:

echo "Replace this line with the mv command".


The suggestion I gave you is more of a debugging test. It helps you see clearly what's happening so you can catch possible mistakes before you use the script on anything important. Sticking in echo lines for variables and commands is a good way to see what your code is really doing.
sorry, i do not know what is positional parameters, actually, i do not completely understand. could you tell me ?thanks

Last edited by topheraholic; 02-26-2009 at 09:58 PM.
 
Old 02-26-2009, 11:04 PM   #33
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
THIS is why i love the internets!
 
Old 02-27-2009, 06:26 AM   #34
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
The positional parameters are the special variables that let you pass arguments to the script from the command line. "$0" is the name of the script itself, "$1" is the first argument, "$2" is the second argument, and so on. "$@" will list all of the parameters together and "$#" will give you the total number of arguments (excluding $0).

For example:
Code:
#!/bin/bash

if [ $# -gt 0  ]; then

        echo "The script name is: $0"
        echo "The total number of arguments is: $#"

else

        echo "You must supply at least one argument"
        echo -e "example: \"$0 foo bar bum\""
        exit 1

fi

echo ""
echo "Here are the first three arguments:"
echo ""
echo "The first argument is $1"
echo "The second argument is ${2:-empty}"
echo "The third argument is ${3:-empty}" 
echo ""
echo "The full list of arguments is: $@"

exit 0
And when I run it I get:

Code:
~/$ ./testscript.sh
You must supply at least one argument
example: "./testscript.sh foo bar bum"


~/$ ./testscript.sh Hello World
The script name is: ./testscript.sh
The total number of arguments is: 2

Here are the first three arguments:

The first argument is Hello
The second argument is World
The third argument is empty

The full list of arguments is: Hello World
If you're wondering about "${2:-empty}", it's a way to set a default value for a variable: ${var:-string} = if $var doesn't exist, use "string" instead.

"echo -e" allows you to use characters such as quotes in the string by escaping them with backslashes.

Last edited by David the H.; 02-27-2009 at 06:29 AM.
 
Old 03-02-2009, 07:34 AM   #35
topheraholic
Member
 
Registered: Aug 2008
Location: shanghai
Distribution: ubuntu
Posts: 128

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by David the H. View Post
The positional parameters are the special variables that let you pass arguments to the script from the command line. "$0" is the name of the script itself, "$1" is the first argument, "$2" is the second argument, and so on. "$@" will list all of the parameters together and "$#" will give you the total number of arguments (excluding $0).

For example:
Code:
#!/bin/bash

if [ $# -gt 0  ]; then

        echo "The script name is: $0"
        echo "The total number of arguments is: $#"

else

        echo "You must supply at least one argument"
        echo -e "example: \"$0 foo bar bum\""
        exit 1

fi

echo ""
echo "Here are the first three arguments:"
echo ""
echo "The first argument is $1"
echo "The second argument is ${2:-empty}"
echo "The third argument is ${3:-empty}" 
echo ""
echo "The full list of arguments is: $@"

exit 0
And when I run it I get:

Code:
~/$ ./testscript.sh
You must supply at least one argument
example: "./testscript.sh foo bar bum"


~/$ ./testscript.sh Hello World
The script name is: ./testscript.sh
The total number of arguments is: 2

Here are the first three arguments:

The first argument is Hello
The second argument is World
The third argument is empty

The full list of arguments is: Hello World
If you're wondering about "${2:-empty}", it's a way to set a default value for a variable: ${var:-string} = if $var doesn't exist, use "string" instead.

"echo -e" allows you to use characters such as quotes in the string by escaping them with backslashes.
oh thanks! but now, i still donot know how to rename more than one file names,could you please tell me more? thanks very much.
 
Old 03-02-2009, 08:37 AM   #36
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
Well, I admit that renaming files is a bit more difficult than some operations, because each output filename needs to be unique. But it's not really that hard. The easiest way is just to take the input filename and make a minor change to it, like changing the ending, which I showed you how to do before, or adding a text string as a prefix or suffix.

Code:
#!/bin/bash

# A simple loop that adds "myphoto-" to the
# beginning of .jpg file names.

for filename in *.jpg; do 

mv $filename myphoto-$filename

done
So if the input files are

01.jpg
02.jpg
03.jpg
foo.jpg
bar.jpg

the output filenames will be

myphoto-01.jpg
myphoto-02.jpg
myphoto-03.jpg
myphoto-foo.jpg
myphoto-bar.jpg

Remember, the variable "filename" in this case simply holds the name of the file. Just imagine the real name appearing everywhere you see "$filename" in the command, input and output. That's all there is to it.

For more complex naming, it would help if you would make it clear just what you want the output filenames to look like. Different techniques may be required for different kinds of output.
 
Old 03-06-2009, 10:09 AM   #37
topheraholic
Member
 
Registered: Aug 2008
Location: shanghai
Distribution: ubuntu
Posts: 128

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by David the H. View Post
Well, I admit that renaming files is a bit more difficult than some operations, because each output filename needs to be unique. But it's not really that hard. The easiest way is just to take the input filename and make a minor change to it, like changing the ending, which I showed you how to do before, or adding a text string as a prefix or suffix.

Code:
#!/bin/bash

# A simple loop that adds "myphoto-" to the
# beginning of .jpg file names.

for filename in *.jpg; do 

mv $filename myphoto-$filename

done
So if the input files are

01.jpg
02.jpg
03.jpg
foo.jpg
bar.jpg

the output filenames will be

myphoto-01.jpg
myphoto-02.jpg
myphoto-03.jpg
myphoto-foo.jpg
myphoto-bar.jpg

Remember, the variable "filename" in this case simply holds the name of the file. Just imagine the real name appearing everywhere you see "$filename" in the command, input and output. That's all there is to it.

For more complex naming, it would help if you would make it clear just what you want the output filenames to look like. Different techniques may be required for different kinds of output.
how to generate sequential number?

could i ask you one other thing?
my major is not computer, i learn it by myself at spare time,but i wanna do it good and better, so i was thinking to take some classes. maybe like CCNA CCNP RHCA etc. so what you think? i thought you are good at this, so it will be great of you to give me some advices. thanks in advance! i appreciate!

Last edited by topheraholic; 03-06-2009 at 10:13 AM.
 
Old 03-08-2009, 01:16 AM   #38
topheraholic
Member
 
Registered: Aug 2008
Location: shanghai
Distribution: ubuntu
Posts: 128

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by David the H. View Post
Well, I admit that renaming files is a bit more difficult than some operations, because each output filename needs to be unique. But it's not really that hard. The easiest way is just to take the input filename and make a minor change to it, like changing the ending, which I showed you how to do before, or adding a text string as a prefix or suffix.

Code:
#!/bin/bash

# A simple loop that adds "myphoto-" to the
# beginning of .jpg file names.

for filename in *.jpg; do 

mv $filename myphoto-$filename

done
So if the input files are

01.jpg
02.jpg
03.jpg
foo.jpg
bar.jpg

the output filenames will be

myphoto-01.jpg
myphoto-02.jpg
myphoto-03.jpg
myphoto-foo.jpg
myphoto-bar.jpg

Remember, the variable "filename" in this case simply holds the name of the file. Just imagine the real name appearing everywhere you see "$filename" in the command, input and output. That's all there is to it.

For more complex naming, it would help if you would make it clear just what you want the output filenames to look like. Different techniques may be required for different kinds of output.
do you know other good books or something about shell? it seems simple and easy,but why i can not accomplish it by myself? thanks
 
Old 03-08-2009, 03:41 AM   #39
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
I'm not sure how much more I can help you. You've already seen LinuxCommand, which is the easiest beginner's tutorial I know about, and I've guided you through a dozen posts of simple scripts. I don't see how anything else I can give you would be of benefit.

You seem to be getting lost on something very basic and fundamental about scripting, perhaps about how variables and embedded commands work. If you don't understand the concept of variables and substituting one thing for another, then you're not going to progress very far. The examples I've given you so far are for the most part very simple scripts that require only the use of variables, for loops and basic system commands. You need to stop here and work on what's already been covered until you clearly understand how it works, before moving on to anything else.

You also appear to need to familiarize yourself more with how basic cli commands such as mv work. If you don't know how to use the tools, then you can't accomplish anything using them.

Finally, there's a wealth of scripting information out there, some of which has already been given to you. LinuxCommand.org, the scripting section of the Rute User's Tutorial, the tdlp Bash HOWTO, Bash Guide for Beginners, and Advanced Bash Scripting Guide, and more.

You just have to keep plugging away until you understand it.
 
Old 03-08-2009, 08:16 AM   #40
topheraholic
Member
 
Registered: Aug 2008
Location: shanghai
Distribution: ubuntu
Posts: 128

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by David the H. View Post
I'm not sure how much more I can help you. You've already seen LinuxCommand, which is the easiest beginner's tutorial I know about, and I've guided you through a dozen posts of simple scripts. I don't see how anything else I can give you would be of benefit.

You seem to be getting lost on something very basic and fundamental about scripting, perhaps about how variables and embedded commands work. If you don't understand the concept of variables and substituting one thing for another, then you're not going to progress very far. The examples I've given you so far are for the most part very simple scripts that require only the use of variables, for loops and basic system commands. You need to stop here and work on what's already been covered until you clearly understand how it works, before moving on to anything else.

You also appear to need to familiarize yourself more with how basic cli commands such as mv work. If you don't know how to use the tools, then you can't accomplish anything using them.

Finally, there's a wealth of scripting information out there, some of which has already been given to you. LinuxCommand.org, the scripting section of the Rute User's Tutorial, the tdlp Bash HOWTO, Bash Guide for Beginners, and Advanced Bash Scripting Guide, and more.

You just have to keep plugging away until you understand it.
thanks!
 
  


Reply



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
How to run root privileged Linux command as normal user via shell shell tcegrid Linux - Newbie 1 06-23-2008 03:38 PM
LXer: Terminal functions for shell scripting with Shell Curses LXer Syndicated Linux News 0 03-26-2008 11:50 PM
LXer: Shell tip: Set the shell prompt and themes in Linux Terminal LXer Syndicated Linux News 0 06-12-2007 03:02 AM
Alias or shell script to confirm 'exit' commands from a shell rose_bud4201 Programming 2 03-08-2006 02:34 PM
'sh' shell - Actually calls legacy Bourne shell, or uses system default? Dtsazza Linux - Software 1 10-28-2005 09:20 AM

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

All times are GMT -5. The time now is 10:55 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