LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   what can i do with shell? (https://www.linuxquestions.org/questions/linux-newbie-8/what-can-i-do-with-shell-704408/)

topheraholic 02-25-2009 07:52 AM

Quote:

Originally Posted by David the H. (Post 3452390)
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.

topheraholic 02-26-2009 09:57 PM

Quote:

Originally Posted by David the H. (Post 3452390)
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

JulianTosh 02-26-2009 11:04 PM

THIS is why i love the internets!

David the H. 02-27-2009 06:26 AM

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.

topheraholic 03-02-2009 07:34 AM

Quote:

Originally Posted by David the H. (Post 3459273)
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.

David the H. 03-02-2009 08:37 AM

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.

topheraholic 03-06-2009 10:09 AM

Quote:

Originally Posted by David the H. (Post 3462394)
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!

topheraholic 03-08-2009 01:16 AM

Quote:

Originally Posted by David the H. (Post 3462394)
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

David the H. 03-08-2009 03:41 AM

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.

topheraholic 03-08-2009 08:16 AM

Quote:

Originally Posted by David the H. (Post 3468425)
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!:)


All times are GMT -5. The time now is 04:55 PM.