LinuxQuestions.org
Help answer threads with 0 replies.
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 10-17-2016, 07:43 AM   #16
NewGuy95
LQ Newbie
 
Registered: Oct 2016
Posts: 14

Original Poster
Rep: Reputation: Disabled

Code:
#!/bin/bash

rows=""
name=""
for f in $1
do
	[ -f "$f" ] || continue
	cnt='wc -l < "$f"'
	if [ -z "$rows" ] || [ $cnt -lt $rows ]
		then
		rows=$cnt
		name=$f
	fi
done
echo "$file has $rows
 
Old 10-17-2016, 07:55 AM   #17
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,603
Blog Entries: 4

Rep: Reputation: 3895Reputation: 3895Reputation: 3895Reputation: 3895Reputation: 3895Reputation: 3895Reputation: 3895Reputation: 3895Reputation: 3895Reputation: 3895Reputation: 3895
Thanks. There are three hard to see typos. The first is that single quotes ( ' ) were used instead of backticks ( ` ) That is one reason why it is a good idea to do command substitution with parenthesis instead. The second is the wrong variable name in the final line and missing a closing double quote.

Code:
#!/bin/bash

rows=""
name=""
for f in $1
do
	[ -f "$f" ] || continue
	cnt=$(wc -l < "$f")
	if [ -z "$rows" ] || [ $cnt -lt $rows ]
		then
		rows=$cnt
		name=$f
	fi
done
echo "$name has $rows"
Note that the script will try to look at all files in the given directory, even if they are subdirectories or binaries.
 
2 members found this post helpful.
Old 10-17-2016, 08:29 AM   #18
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,470

Rep: Reputation: 2799Reputation: 2799Reputation: 2799Reputation: 2799Reputation: 2799Reputation: 2799Reputation: 2799Reputation: 2799Reputation: 2799Reputation: 2799Reputation: 2799
Quote:
Name of directory will be passed as argument, also when there will be no arguments than script should return error (like echo "You forgot arument").
and
Quote:
I don't know where to put directory name?
For this you want to pass a positional parameter.
If you call your script and supply the directory name e.g. scriptname.sh </your/directory/name>
then I suggest a construction like:
Code:
if [ -d "$1"] ; then
  cd "$1"
else
  echo "Invalid directory name - exiting. Usage: ${0##*/} <directory>"
  exit(1)
fi
The '${0##*/}' is a bash parameter expansion that finesses the $0 positional parameter to leave just the script name.
 
1 members found this post helpful.
Old 10-17-2016, 09:44 AM   #19
NewGuy95
LQ Newbie
 
Registered: Oct 2016
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by allend View Post
and


For this you want to pass a positional parameter.
If you call your script and supply the directory name e.g. scriptname.sh </your/directory/name>
then I suggest a construction like:
Code:
if [ -d "$1"] ; then
  cd "$1"
else
  echo "Invalid directory name - exiting. Usage: ${0##*/} <directory>"
  exit(1)
fi
The '${0##*/}' is a bash parameter expansion that finesses the $0 positional parameter to leave just the script name.
Can you be a little bit more specific where to put that code?
After
Code:
 for f in $1
do ....
or where?

Thank you all for help!
 
Old 10-17-2016, 09:59 AM   #20
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,603
Blog Entries: 4

Rep: Reputation: 3895Reputation: 3895Reputation: 3895Reputation: 3895Reputation: 3895Reputation: 3895Reputation: 3895Reputation: 3895Reputation: 3895Reputation: 3895Reputation: 3895
The square brace is another way of writing "test", which is built-in with "bash"

It checks the existence of the directory you are going to be looking for. The manual page for "bash" is daunting. Really. But over time you'll learn to navigate it. See if you can hunt for "test" and its option -d in there:

Code:
man bash
It's under "conditional expressions"
So you'll need that test in your script before you try to use the directory.

Try it in a separate script:

Code:
#!/bin/bash

if [ -d "$1"] ; then
  cd "$1"
else
  echo "Invalid directory name - exiting. Usage: ${0##*/} directory"
  exit(1)
fi

exit(0)
Edit: fixed shell

Last edited by Turbocapitalist; 10-17-2016 at 10:20 AM.
 
Old 10-17-2016, 10:14 AM   #21
NewGuy95
LQ Newbie
 
Registered: Oct 2016
Posts: 14

Original Poster
Rep: Reputation: Disabled
I get errors at
Code:
 exit(1)
line

But if I try to paste those if statements inside my code it is working with directory, but without directory it displays me error like 20 times in a row.

Code:
#!/bin/bash

rows=""
name=""
for f in $1/*
do
        if [ -d "$1" ] ; then
	[ -f "$f" ] || continue
	cnt=$(wc -l < "$f")
	if [ -z "$rows" ] || [ $cnt -lt $rows ]
		then
		rows=$cnt
		name=$f
	fi
        else
           echo "Invalid directory name - exiting. Usage: ${0##*/} <directory>"
        fi
done
echo "$name has $rows"
 
Old 10-17-2016, 10:22 AM   #22
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,603
Blog Entries: 4

Rep: Reputation: 3895Reputation: 3895Reputation: 3895Reputation: 3895Reputation: 3895Reputation: 3895Reputation: 3895Reputation: 3895Reputation: 3895Reputation: 3895Reputation: 3895
The for and done mark a loop. Stuff inside that will be repeated for each file found. Try moving the test before it, since you will need to test the directory.


PS. I fixed the shell in the script above, since we're sticking with "bash" for this task.
 
Old 10-17-2016, 10:30 AM   #23
NewGuy95
LQ Newbie
 
Registered: Oct 2016
Posts: 14

Original Poster
Rep: Reputation: Disabled
Code:
#!/bin/bash

rows=""
name=""
if [ -d "$1" ] ; then
   cd "$1"
else
   echo "Invalid directory name - exiting. Usage: ${0##*/} <directory>"
fi
for f in $1/*
do
       
	[ -f "$f" ] || continue
	cnt=$(wc -l < "$f")
	if [ -z "$rows" ] || [ $cnt -lt $rows ]
		then
		rows=$cnt
		name=$f
	fi
done
echo "$name has $rows"
If I try this way, than I again got
Code:
     has    rows
in other way, when I should get error I got
Code:
Row 13: /vmlinuz: Permission denied
Row 14: [: -lt: Expecting simple operator (Idk translation again)
/vmlinuz.old has 24188 rows.
 
Old 10-17-2016, 10:36 AM   #24
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,603
Blog Entries: 4

Rep: Reputation: 3895Reputation: 3895Reputation: 3895Reputation: 3895Reputation: 3895Reputation: 3895Reputation: 3895Reputation: 3895Reputation: 3895Reputation: 3895Reputation: 3895
Where did the exit 1 go and why is it needed?

Also, a test is needed to make sure that $1 is not empty.
 
Old 10-17-2016, 10:42 AM   #25
NewGuy95
LQ Newbie
 
Registered: Oct 2016
Posts: 14

Original Poster
Rep: Reputation: Disabled
Like I told you before I got error if I use exit(1) in row 9 (where exit is) and it wont let me any further.
Exit (1) (usually) indicates unsucessful termination if I got it right
 
Old 10-17-2016, 10:44 AM   #26
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,028

Rep: Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200
I'm kind of curious at what point the OP is going to do any work? So far he/she has been spoon fed every line of code and as far as I can see done zero to work anything out except ask another question
on how to fix code that is clearly not understood.

Maybe the OP would be better served by searching for the solutions instead of asking for a hand out. I would add that running a script on your system when you have no idea what it is doing is fraught with
danger.

Turbocapitalist has provided the 'wooledge' website which if processed would cover off many of the questions being asked. You have also been told repeatedly that operating on non-text files (see your
latest error messages) will cause issues.
 
Old 10-17-2016, 10:48 AM   #27
NewGuy95
LQ Newbie
 
Registered: Oct 2016
Posts: 14

Original Poster
Rep: Reputation: Disabled
Dear grail, thanks for your response.
As an non-english speaker I find it more simple if someone can explain me my problem in a way I will understand what is going on. As a totaly newbie (like few days) I don't understand all terms in literature that has been given to me.

Thats why I am asking experts like you guys are for some help. I you find it too hard to help, than please avoid this topic
 
Old 10-17-2016, 11:19 AM   #28
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,028

Rep: Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200
I have no issue with helping and am happy to see you take your first steps in this arena, but currently I have not seen any feedback from you that was not written by someone else and you will
not learn by regurgitating someone else's code / issues.

The comments you have made show that you are not completely understanding the first 'if' clause and why it requires the exit command. You should get to grips with this first and understand
what entries make you hit either part of the 'if' clause and what you need to enter at the command line to progress correctly past this structure.

Once you have understood the above it may then be prudent to walk your self the same way through each part of the code inside the 'for loop' as again, the questions you are coming up against are from a lack of understanding which others will fix but you will become no better at coding in bash without understanding it.

I would reiterate my earlier information too in that you really need to click on every link and work your way through the entire 'wooledge' site as the information is invaluable. I know this will be daunting as the sire is only in English, but the concepts are sound and will help you should you learn what is there

Good luck ... and I hope you keep at it
 
1 members found this post helpful.
Old 10-17-2016, 11:33 AM   #29
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Does it have to be a script? This is really a one-liner kind of thing:

Code:
find /root/ -type f -exec wc -l {} \; | sort -g > sorted_list; clear; tail -n 1 sorted_list
If you want to break that out into a whole script, with parameters:

Code:
#!/bin/bash
if [ -d "$1" ] ; then
find $1 -type f -exec wc -l {} \; | sort -g > /tmp/sorted_list; clear; tail -n 1 /tmp/sorted_list   
else
echo "USAGE: ./sortbylines.sh [path to directory]"
echo ""
fi
 
Old 10-17-2016, 03:37 PM   #30
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
Quote:
What if there are other text files like .odt ?
...
OpenDocument Format files have text in them but it is buried in XML which is in turn archived in a ZIP archive, so it's not a straight up task to read the number of lines in them.
Once you learn a lot of the terminal commands to manage plain text, a frequent task is figuring out to convert those "complex" files like xml into a more readable plain text.
You will lose a lot / all formatting data but this is typically not needed (finding number of lines, grepping for strings, etc)

In this case, odt2txt can be used to convert odt files to text files
https://stackoverflow.com/questions/...s-to-txt-files
 
  


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
Creating script which find the file with the less rows NewGuy95 Linux - Newbie 1 10-17-2016 02:25 PM
[SOLVED] Find a File delete out different file in same directory HardenedCriminal Linux - Security 9 06-04-2015 08:28 PM
find the largest and smallest file in a directory and sub directory schandran Programming 5 05-06-2015 10:53 AM
how to find match between two rows of column in same file using PERL genetist Linux - Newbie 3 07-13-2013 12:17 PM
a2ps: cannot find file `c0419bt_.afm': No such file or directory rg3 Slackware 5 08-02-2009 04:11 PM

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

All times are GMT -5. The time now is 09:42 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
Open Source Consulting | Domain Registration