LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 06-08-2016, 05:16 PM   #1
hexle
LQ Newbie
 
Registered: Jun 2016
Posts: 12

Rep: Reputation: Disabled
I have a code but i cant understand it .Can you help me?


Hello, i have been in some sites trying to help me out with my problem.I am not good at linux.I have try a lot and i have obtain some knowleges..I had a "homework" and i wasnt and many could solve it.What i did was to find a guy and ask him if he could solve it and he did it.I have make comments on the code .Comments that i cant understand it.Please explain me what it does on that spot.
Code:
if test $# -eq 0 ; then //what it does here? test equal to 0? 
   echo "USAGE ---> $0 file [ -topip4 ] [ -topip6 ][ -topurl ]" >&2 // it send to exit 1 
   exit 1
   fi
   top4=0 // why give 0?to all this?
   top6-0
   topurl=0
   file=' '  // why file ? and why file = ' ' ? 
   for i in $a ; do 
   case "$i" in 
   -topip4) top4=1;; // what means -topip4) ? 
   -topip6) top6=1;;
   -topurl) topurl=1;;
   -*) echo "USAGE ---> $0file [ -topip4 ] [ -topip6 ] [ -topurl ]">&2   // why -*)  ? 
   echo "Unknown flag $i" 
   exit 2;;
   *) if [ -n "$file" ];then  
   echo "Multiple file $file, $i" >&2
   exit3
   *) file = "$i";; 
   esac //what is this?
   done
   if [ ! -f "$file" ]; then  // if not is a file ?
   echo "$file not a file" >&2  
   exit 2
   fi
   if [ $top4 -eq 0 - a $top6 -eq 0 -a $topurl -eq 0 ]; then // what does - a here? 
   top4=1
   top6=1
   topurl=1
   fi
   *)if [ -n "$file" ]; then
   echo "Multiple file $file, $i" >&2
   exit 3
   *) file= "$i";;
   esac // what this is?
   done 
   if[ ! -f "$file" ]; then
   echo "$file not a file " >&2 
   exit 2
   fi
   if [ $top4 -eq 0 - a $top6 -eq 0 - a $topurl -eq 0 ]; then // why this is again? writen?
   top4=1
   top6=1
   topurl=1
   fi 
   #echo "DEBUG:$top4,$top6,$topurl,$file"
   if ((top4));then 
   echo -n "Top IP4 ---> "
   egrep "^[0-9]{1,3}\." "$file"|cut -d ' ' -f1|sort|uniq -c|sort -nr|sed -n '1p'|sed 's/^.*//'  // i cant understand it all
   fi
   if ((top6)); then
   echo -n "Top IP6 ---> "
   egrep "^[0-9-a-f]*\." "$file"|cut -d ' ' -f1|sort|uniq -c|sort -nr|sed -n '1p'|sed 's/^.*//'
   fi
   if ((topurl));then 
   echo -n "Top Url --->"
   cut -d ' ' -f7 "$file"|sed -r '/.[/]$/ s/[/]$//'|sort|uniq -c|sort -n|sed- n '$p'|sed ' s/.*//' //i cant understand it too all
 
Old 06-08-2016, 06:47 PM   #2
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 16,825

Rep: Reputation: 2408Reputation: 2408Reputation: 2408Reputation: 2408Reputation: 2408Reputation: 2408Reputation: 2408Reputation: 2408Reputation: 2408Reputation: 2408Reputation: 2408
What can you not understand ?

break it into parts

what dose EACH part do
 
1 members found this post helpful.
Old 06-08-2016, 07:43 PM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,944

Rep: Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693
Quote:
Originally Posted by hexle View Post
Hello, i have been in some sites trying to help me out with my problem.I am not good at linux.I have try a lot and i have obtain some knowleges..I had a "homework" and i wasnt and many could solve it.What i did was to find a guy and ask him if he could solve it and he did it.I have make comments on the code .Comments that i cant understand it.Please explain me what it does on that spot.
Code:
if test $# -eq 0 ; then //what it does here? test equal to 0? 
   echo "USAGE ---> $0 file [ -topip4 ] [ -topip6 ][ -topurl ]" >&2 // it send to exit 1 
   exit 1
   fi
   top4=0 // why give 0?to all this?
   top6-0
   topurl=0
   file=' '  // why file ? and why file = ' ' ? 
   for i in $a ; do 
   case "$i" in 
   -topip4) top4=1;; // what means -topip4) ? 
   -topip6) top6=1;;
   -topurl) topurl=1;;
   -*) echo "USAGE ---> $0file [ -topip4 ] [ -topip6 ] [ -topurl ]">&2   // why -*)  ? 
   echo "Unknown flag $i" 
   exit 2;;
   *) if [ -n "$file" ];then  
   echo "Multiple file $file, $i" >&2
   exit3
   *) file = "$i";; 
   esac //what is this?
   done
   if [ ! -f "$file" ]; then  // if not is a file ?
   echo "$file not a file" >&2  
   exit 2
   fi
   if [ $top4 -eq 0 - a $top6 -eq 0 -a $topurl -eq 0 ]; then // what does - a here? 
   top4=1
   top6=1
   topurl=1
   fi
   *)if [ -n "$file" ]; then
   echo "Multiple file $file, $i" >&2
   exit 3
   *) file= "$i";;
   esac // what this is?
   done 
   if[ ! -f "$file" ]; then
   echo "$file not a file " >&2 
   exit 2
   fi
   if [ $top4 -eq 0 - a $top6 -eq 0 - a $topurl -eq 0 ]; then // why this is again? writen?
   top4=1
   top6=1
   topurl=1
   fi 
   #echo "DEBUG:$top4,$top6,$topurl,$file"
   if ((top4));then 
   echo -n "Top IP4 ---> "
   egrep "^[0-9]{1,3}\." "$file"|cut -d ' ' -f1|sort|uniq -c|sort -nr|sed -n '1p'|sed 's/^.*//'  // i cant understand it all
   fi
   if ((top6)); then
   echo -n "Top IP6 ---> "
   egrep "^[0-9-a-f]*\." "$file"|cut -d ' ' -f1|sort|uniq -c|sort -nr|sed -n '1p'|sed 's/^.*//'
   fi
   if ((topurl));then 
   echo -n "Top Url --->"
   cut -d ' ' -f7 "$file"|sed -r '/.[/]$/ s/[/]$//'|sort|uniq -c|sort -n|sed- n '$p'|sed ' s/.*//' //i cant understand it too all
So...he did part of your homework, and you want us to finish it? When are YOU going to do anything? YOU need to learn this, not us...why don't you put your comments in, and then ask us if you're correct.

But asking one person for a handout, then asking others to give you ANOTHER handout, so you can pass of work as your own, is amazingly lazy.

Last edited by TB0ne; 06-08-2016 at 07:54 PM.
 
2 members found this post helpful.
Old 06-08-2016, 08:17 PM   #4
dijetlo
Senior Member
 
Registered: Jan 2009
Location: RHELtopia....
Distribution: Solaris 11.2/Slackware/RHEL/
Posts: 1,307
Blog Entries: 2

Rep: Reputation: Disabled
you could set -x at the top of the script.
Then it will tell you what it's doing.
 
1 members found this post helpful.
Old 06-08-2016, 08:31 PM   #5
notKlaatu
Member
 
Registered: Sep 2010
Location: Wellington, New Zealand
Distribution: Slackware
Posts: 902

Rep: Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555
Seems like maybe you should start from the beginning and learn the basics. This script has a lot going on, but they are all basic concepts that you should know well enough to piece together. If your homework is requiring this level of scripting, then presumably you've been studying scripting and should know most of this.

Heck, your comments are not even using the correct comment characters.

Even so, a lot of your comments are actually correct. Maybe you know more than you think.

Code:
if test $# -eq 0 ; then   # what it does here? test equal to 0?
Yes, this tests to see if any arguments were passed to the script when it was launched. See Special Variables here: http://hyperpolyglot.org/unix-shells

Code:
   echo "USAGE ---> $0 file [ -topip4 ] [ -topip6 ][ -topurl ]" >&2 // it send to exit 1 
   exit 1
   fi
Again, you're correct. It sends it to exit with an error (1) instead of silently (0) so that you and the computer know that the task was not completed as expected. The reason is because all it did was print a HELP message. It didn't actually run the script..or much of it, anyway.

[code]
file=' ' // why file ? and why file = ' ' ?
[/code0

Setting up a variable called 'file'. It doesn't have to be called 'file'. It could be called 'penguin'. It's a variable name.

I believe it is being set to ' ' just so it's set to a known baseline. I didn't write the script, so I don't know for sure.

Code:
   for i in $a ; do 

   case "$i" in 
   -topip4) top4=1;; // what means -topip4) ? 
   -topip6) top6=1;;
   blah blah blah
   ...
   *) file = "$i";; 
   esac //what is this?

   done
This is case statement wrapped in a for loop.

Read about 'for' loops at http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-7.html

Read about 'case' statements at http://tldp.org/LDP/Bash-Beginners-G...ect_07_03.html

Code:
   if [ ! -f "$file" ]; then  // if not is a file ?
   ...
   if [ $top4 -eq 0 - a $top6 -eq 0 -a $topurl -eq 0 ]; then // what does - a here? 
   ... 
   *)if [ -n "$file" ]; then
   ...
   esac // what this is?
   if ((top6)); then
   ...
Most of these are 'test' statements. Read about test conditions with `man test` from a Unix shell.

[code]
cut -d ' ' -f7 "$file"|sed -r '/.[/]$/ s/[/]$//'|sort|uniq -c|sort -n|sed- n '$p'|sed ' s/.*//' //i cant understand it too all
[/cut]

This is using regular expressions and sed to extract specific strings from whatever file you are pointing the script at.

Read about regex, and practise using it, here -> http://regexone.com/

Read about sed here -> http://www.tldp.org/LDP/abs/html/x23170.html

Good luck!
 
3 members found this post helpful.
Old 06-09-2016, 04:58 AM   #6
hexle
LQ Newbie
 
Registered: Jun 2016
Posts: 12

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by notKlaatu View Post
Seems like maybe you should start from the beginning and learn the basics. This script has a lot going on, but they are all basic concepts that you should know well enough to piece together. If your homework is requiring this level of scripting, then presumably you've been studying scripting and should know most of this.

Heck, your comments are not even using the correct comment characters.

Even so, a lot of your comments are actually correct. Maybe you know more than you think.

Code:
if test $# -eq 0 ; then   # what it does here? test equal to 0?
Yes, this tests to see if any arguments were passed to the script when it was launched. See Special Variables here: http://hyperpolyglot.org/unix-shells

Code:
   echo "USAGE ---> $0 file [ -topip4 ] [ -topip6 ][ -topurl ]" >&2 // it send to exit 1 
   exit 1
   fi
Again, you're correct. It sends it to exit with an error (1) instead of silently (0) so that you and the computer know that the task was not completed as expected. The reason is because all it did was print a HELP message. It didn't actually run the script..or much of it, anyway.

[code]
file=' ' // why file ? and why file = ' ' ?
[/code0

Setting up a variable called 'file'. It doesn't have to be called 'file'. It could be called 'penguin'. It's a variable name.

I believe it is being set to ' ' just so it's set to a known baseline. I didn't write the script, so I don't know for sure.

Code:
   for i in $a ; do 

   case "$i" in 
   -topip4) top4=1;; // what means -topip4) ? 
   -topip6) top6=1;;
   blah blah blah
   ...
   *) file = "$i";; 
   esac //what is this?

   done
This is case statement wrapped in a for loop.

Read about 'for' loops at http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-7.html

Read about 'case' statements at http://tldp.org/LDP/Bash-Beginners-G...ect_07_03.html

Code:
   if [ ! -f "$file" ]; then  // if not is a file ?
   ...
   if [ $top4 -eq 0 - a $top6 -eq 0 -a $topurl -eq 0 ]; then // what does - a here? 
   ... 
   *)if [ -n "$file" ]; then
   ...
   esac // what this is?
   if ((top6)); then
   ...
Most of these are 'test' statements. Read about test conditions with `man test` from a Unix shell.

[code]
cut -d ' ' -f7 "$file"|sed -r '/.[/]$/ s/[/]$//'|sort|uniq -c|sort -n|sed- n '$p'|sed ' s/.*//' //i cant understand it too all
[/cut]

This is using regular expressions and sed to extract specific strings from whatever file you are pointing the script at.

Read about regex, and practise using it, here -> http://regexone.com/

Read about sed here -> http://www.tldp.org/LDP/abs/html/x23170.html

Good luck!
First i am sorry for not write it .I didnt post this exercise to solve it my mistake .I just wanted to field my blanks thats why i came here.I was wrong with the comments .I am doing java and i use // and i think that was the character for comments.I am sorry i didnt knew.Anyway , i have some questions about what you wrote me .
Initial,Thank you for answer me my comments.I havent problem with for,case,if loops i didnt understand why give 1 ?i talk about this :
-topip4) top4=1;; .
The second question is :
Code:
if [ ! -f "$file" ]; then  // if not is a file ?
   ...
   if [ $top4 -eq 0 - a $top6 -eq 0 -a $topurl -eq 0 ]; then // what does - a here? 
   ... 
   *)if [ -n "$file" ]; then
   ...
   esac // what this is?
   if ((top6)); then
   ...
what does -a in this case i cant understand.
third question :
Code:
cut -d ' ' -f7 "$file"|sed -r '/.[/]$/ s/[/]$//'|sort|uniq -c|sort -n|sed- n '$p'|sed ' s/.*//'
the site you gave me i had worked a lot time ago and it was very good it helped me a lot but here i am losing it.I will show you
cut : read
-d : directory
' ' : i dont know here ,maybe want to refer to the file .no idea
-f7: -f i know is file.7 i dont know .
"$file" : $ we write first this and after the variable .We put " " because we want to take exactly the variable as it is.
| :
sed : the way to make changes on a file
-r : ERE
' ' : to refer to sed what it should do
/. : it starts with /.
[/]$ : $ that means the end of the word .he use [/] to take only the /
: i have no idea why he has space here
s/ : i dont know ,but something to do all this
[/]$ : again the same as i discribe it previously
// : i dont know
sort : lets say like bubblesort,invertion sort to make it sort example a,b,c,d..
uniq -c : no idea what it is
sort -n : the sort to be in a line
sed- n '$p' : that means to find the variable p
|sed '
s/ : logic it ends here the s/
.* : what ever it can be digit or word
//' :it ends? no idea
 
Old 06-09-2016, 10:34 AM   #7
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,944

Rep: Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693
Quote:
Originally Posted by hexle View Post
First i am sorry for not write it .I didnt post this exercise to solve it my mistake .I just wanted to field my blanks thats why i came here.I was wrong with the comments .I am doing java and i use // and i think that was the character for comments.I am sorry i didnt knew.
This is EXACTLY THE POINT...this is your homework. You got someone else to write it for you, and you're now asking us to do the rest of your work, because you need comments in it. Again, at what point are you going to learn, if others do your work for you????
Quote:
Anyway , i have some questions about what you wrote me. Initial,Thank you for answer me my comments.
..and just saying "no idea" and "what does this do?" still doesn't help you learn.

This is now the part where you pull out your textbook, and start learning and thinking for yourself, by researching what each line does. Look them up....read the man pages, look at the documentation.
 
Old 06-09-2016, 10:49 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,253

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
I know some times TB0ne can be a little direct (I suffer from this also), but I believe he has pointed out valid points that you are asking yet another person to now work on what
someone else has done ... where is your effort?

As advised by others above, simply break up the code into individual lines and work from one to the next. As you are learning java, the concept of if/while/for/case/... should be familiar enough
that all you need do is work on what each command is doing, ie. sed/cut/uniq/sort/... (all the rest is assignments and conditional tests)
 
1 members found this post helpful.
Old 06-09-2016, 03:49 PM   #9
ondoho
Senior Member
 
Registered: Dec 2013
Posts: 4,176

Rep: Reputation: 837Reputation: 837Reputation: 837Reputation: 837Reputation: 837Reputation: 837Reputation: 837
hexle, instead of desperately trying to understand something that is way above your head, start with something smaller that you can grasp.
learn bash from the ground up!
maybe this can help you:
https://www.howtoforge.com/tutorial/...pting-lessons/
 
Old 06-09-2016, 04:07 PM   #10
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,387

Rep: Reputation: 397Reputation: 397Reputation: 397Reputation: 397
Quote:
Originally Posted by TB0ne View Post
..and just saying "no idea" and "what does this do?" still doesn't help you learn.
Some of the comments look funny to me
 
Old 06-10-2016, 03:28 PM   #11
hexle
LQ Newbie
 
Registered: Jun 2016
Posts: 12

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
I know some times TB0ne can be a little direct (I suffer from this also), but I believe he has pointed out valid points that you are asking yet another person to now work on what
someone else has done ... where is your effort?

As advised by others above, simply break up the code into individual lines and work from one to the next. As you are learning java, the concept of if/while/for/case/... should be familiar enough
that all you need do is work on what each command is doing, ie. sed/cut/uniq/sort/... (all the rest is assignments and conditional tests)
do you want to see my whole hystoric from google chrome?i have done many efforts .This exercise it was ''difficult'' for me to do it alone.Anyway.
 
Old 06-10-2016, 09:14 PM   #12
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,944

Rep: Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693
Quote:
Originally Posted by hexle View Post
do you want to see my whole hystoric from google chrome?i have done many efforts.
Since you claim to have "done many efforts", then why don't you show what you've done. Again, posting "I have no idea" and "What does this do?" doesn't look like you've put effort forward, but are just asking us to explain what someone else wrote for you.

And if you did put effort into this, you would have, at the VERY LEAST seen that bash scripts don't use // for comments....but you didn't. If you want to learn, then as you've been told SEVERAL TIMES in this thread, look at EACH LINE...and figure out what that line does.
Quote:
This exercise it was ''difficult'' for me to do it alone.Anyway.
So you had someone else do it for you, and want us to finish it?
 
  


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
Danbooru,postgresql,Code. Cant understand this code Vodkaholic1983 Linux - Newbie 7 07-03-2010 04:47 PM
how to understand this shell code? nillgump Linux From Scratch 1 10-10-2008 05:19 AM
help to understand this code cranium2004 Programming 1 03-21-2005 12:35 PM
Help me understand this Driver code! fbarre Linux - Newbie 2 09-24-2003 06:10 PM
Help me understand this Driver code! fbarre Linux - General 2 09-24-2003 01:51 PM


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