Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then 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.
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.
#!/bin/sh
echo "Your string here please"
read USERINPUT #Gets user input
if [ ${#USERINPUT} -lt 10 ]; then #The ${#USERINPUT} gets the length of the string rather than its value
echo "You did not enter a string over Ten characters long."
else
echo "Congrats you did enter a string over ten Characters long."
fi
#!/bin/sh
find -maxdepth 1 -type f | grep -v "rpm" | grep -v "tar.bz2" | grep -v "tar.gz" | grep -v "tgz" | grep -v "myscript" | grep "./[aA0-zZ9]" | sort -n >> lastthree.tmp
#The above very long line will search the directory this script is in for all files. It will exclude however, rpms, tarballs (bz2 and gz type), and all hidden# files (those begining with "."
#This is then directed to lastthree.tmp to be used later.
#Also, grep out the name of the script here, mine was named "myscript"
LOOP=`cat lastthree.tmp | grep -n ^ | tail -1 | cut -c 1`
#The LOOP variable is set the the numerical value of the lines the the created temp file above.
START=1
#Start is set to 1, this is for the command done in the loop
while [ $START -le $LOOP ];
do
echo "This output is from file: " `cat lastthree.tmp | grep -n ^ | grep $START | cut -d : -f2-` >> threeout.tmp
FILENAME=`cat lastthree.tmp | grep -n ^ | grep $START | cut -d : -f2-`
tail -3 $FILENAME >> threeout.tmp
START=`expr $START + 1`
done
#The above loop will go through the file lastthree and get the last three lines
#of the files there. It will then print the file it is on and the last three
#lines of that file. It will then add one to START and
#begin the loop again and do the same for the next file. This is all be stored
#in threeout.tmp
Note you may wish to add a line at the end that removes the "tmp" files it creates. If you need anything explained just post here and I'll be glad to help. Both temp files are created in the directory the script is run in. Also this script is meant to run in the directory you want the file output from. It can quite possilbly so other directories but that would take a small bit of re-tooling.
If you noticed from herve's post he wanted the last three lines of EVERY file in the directory. I would assume that because he wants a script it is because he does not want to do tail -3 on every file (as there could be hundreds)
Also I used tail -3 in my script above.
I will sometimes filter the output of 'locate' by using 'grep -v' and pressing the up arrow and adding more patterns to exclude with '-e pattern.
It may be better to use the patterns -e ".rpm$" -e ".tar.bz2$" -e ".tar.gz$" -e ".tgz$"
to avoid filtering out filenames that happen to have a matching pattern inside the filename.
Also the pattern for hidden files would be "\..*" The backslash is needed to escape the period, because '.' alone represents any character. '.*' means zero or more of any character.
Also, you could use:
for file in `cat lastthree.tmp`
do
tail -3 $FILENAME >> threeout.tmp
done
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.