LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   I have a code but i cant understand it .Can you help me? (https://www.linuxquestions.org/questions/linux-newbie-8/i-have-a-code-but-i-cant-understand-it-can-you-help-me-4175581794/)

hexle 06-08-2016 04:16 PM

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


John VV 06-08-2016 05:47 PM

What can you not understand ?

break it into parts

what dose EACH part do

TB0ne 06-08-2016 06:43 PM

Quote:

Originally Posted by hexle (Post 5557971)
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.

dijetlo 06-08-2016 07:17 PM

you could set -x at the top of the script.
Then it will tell you what it's doing.

notKlaatu 06-08-2016 07:31 PM

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!

hexle 06-09-2016 03:58 AM

Quote:

Originally Posted by notKlaatu (Post 5558077)
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

TB0ne 06-09-2016 09:34 AM

Quote:

Originally Posted by hexle (Post 5558244)
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.

grail 06-09-2016 09:49 AM

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)

ondoho 06-09-2016 02:49 PM

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/

keefaz 06-09-2016 03:07 PM

Quote:

Originally Posted by TB0ne (Post 5558375)
..and just saying "no idea" and "what does this do?" still doesn't help you learn.

Some of the comments look funny to me :D

hexle 06-10-2016 02:28 PM

Quote:

Originally Posted by grail (Post 5558389)
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.

TB0ne 06-10-2016 08:14 PM

Quote:

Originally Posted by hexle (Post 5559012)
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?


All times are GMT -5. The time now is 06:48 PM.