LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 02-25-2005, 04:06 PM   #1
herve2001
Member
 
Registered: Sep 2004
Posts: 34

Rep: Reputation: 15
Shell scripting Problems!


I'm looking for some help from anyone who knows shell scripting, I'm trying to do the following:

1-Accept a string form the terminal and use case to echo a suitable message if the string doesn't have at least 10 characters.

and

2-Write a script that displays, in head-style, the last three lines of each file in the current directory, duly preceded by the filename.


Thanks.
 
Old 02-25-2005, 04:43 PM   #2
caps_phisto
Member
 
Registered: Sep 2004
Location: NH
Distribution: FC6, FC1-4, RH9, Gentoo 2006.0/1, Slackware 10.1/2,11, Vector SOHO 5.0.1
Posts: 237

Rep: Reputation: 30
Hey I got the answer to number one for you:

Code:
#!/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
I'm still working on number two for you.
 
Old 02-25-2005, 06:08 PM   #3
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Take a look at the awk language.
 
Old 02-25-2005, 06:23 PM   #4
caps_phisto
Member
 
Registered: Sep 2004
Location: NH
Distribution: FC6, FC1-4, RH9, Gentoo 2006.0/1, Slackware 10.1/2,11, Vector SOHO 5.0.1
Posts: 237

Rep: Reputation: 30
Hey bud, here is number two:

Code:
#!/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.
 
Old 02-26-2005, 04:34 PM   #5
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
I just remembered a simpler way to do question 2:

Code:
tail -3 file
"tail," like "head" is built in.

Enter
Code:
tail --help
for more info.
 
Old 02-26-2005, 08:14 PM   #6
caps_phisto
Member
 
Registered: Sep 2004
Location: NH
Distribution: FC6, FC1-4, RH9, Gentoo 2006.0/1, Slackware 10.1/2,11, Vector SOHO 5.0.1
Posts: 237

Rep: Reputation: 30
PT,

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.

Quote:
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
 
Old 02-26-2005, 08:50 PM   #7
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
One of the lines on the script in post #4 can be shortened.

find -maxdepth 1 -type f | grep -v "rpm" | grep -v "tar.bz2" | grep -v "tar.gz" | grep -v "tgz" | grep -v "myscript"

find -maxdepth 1 -type f | grep -v -e rpm -e tar.bz2 -e tar.gz -e tgz

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
 
Old 02-26-2005, 08:57 PM   #8
caps_phisto
Member
 
Registered: Sep 2004
Location: NH
Distribution: FC6, FC1-4, RH9, Gentoo 2006.0/1, Slackware 10.1/2,11, Vector SOHO 5.0.1
Posts: 237

Rep: Reputation: 30
Excellent modifications! That would definately make things look and feel cleaner! Thanks for the additional reply!
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
shell interface vs shell scripting? I'm confused jcchenz Linux - Software 1 10-26-2005 03:32 PM
shell scripting help newbie_st Linux - General 5 08-28-2005 02:52 PM
Shell Scripting problems (newbie) yawgmoth81 Programming 11 02-24-2003 02:31 AM
shell scripting mindcry Linux - Software 2 11-26-2002 06:25 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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