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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
|
10-17-2016, 07:43 AM
|
#16
|
LQ Newbie
Registered: Oct 2016
Posts: 14
Original Poster
Rep:
|
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
|
|
|
10-17-2016, 07:55 AM
|
#17
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,603
|
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.
|
10-17-2016, 08:29 AM
|
#18
|
LQ 5k Club
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,470
|
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.
|
10-17-2016, 09:44 AM
|
#19
|
LQ Newbie
Registered: Oct 2016
Posts: 14
Original Poster
Rep:
|
Quote:
Originally Posted by allend
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!
|
|
|
10-17-2016, 09:59 AM
|
#20
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,603
|
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:
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.
|
|
|
10-17-2016, 10:14 AM
|
#21
|
LQ Newbie
Registered: Oct 2016
Posts: 14
Original Poster
Rep:
|
I get errors at 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"
|
|
|
10-17-2016, 10:22 AM
|
#22
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,603
|
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.
|
|
|
10-17-2016, 10:30 AM
|
#23
|
LQ Newbie
Registered: Oct 2016
Posts: 14
Original Poster
Rep:
|
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
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.
|
|
|
10-17-2016, 10:36 AM
|
#24
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,603
|
Where did the exit 1 go and why is it needed?
Also, a test is needed to make sure that $1 is not empty.
|
|
|
10-17-2016, 10:42 AM
|
#25
|
LQ Newbie
Registered: Oct 2016
Posts: 14
Original Poster
Rep:
|
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
|
|
|
10-17-2016, 10:44 AM
|
#26
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,028
|
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.
|
|
|
10-17-2016, 10:48 AM
|
#27
|
LQ Newbie
Registered: Oct 2016
Posts: 14
Original Poster
Rep:
|
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
|
|
|
10-17-2016, 11:19 AM
|
#28
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,028
|
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.
|
10-17-2016, 11:33 AM
|
#29
|
Senior Member
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278
|
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
|
|
|
10-17-2016, 03:37 PM
|
#30
|
Member
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634
|
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
|
|
|
All times are GMT -5. The time now is 09:42 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|