LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Shell scripting Problems! (https://www.linuxquestions.org/questions/linux-general-1/shell-scripting-problems-294879/)

herve2001 02-25-2005 04:06 PM

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.

caps_phisto 02-25-2005 04:43 PM

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.

PTrenholme 02-25-2005 06:08 PM

Take a look at the awk language.

caps_phisto 02-25-2005 06:23 PM

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.

PTrenholme 02-26-2005 04:34 PM

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.

caps_phisto 02-26-2005 08:14 PM

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

jschiwal 02-26-2005 08:50 PM

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

caps_phisto 02-26-2005 08:57 PM

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


All times are GMT -5. The time now is 10:14 PM.