Results 1 to 6 of 6
Help,
I need to write a script that prints the longest filename in a directory. The directory is supplied as an argument to the script. I have no problems supplying ...
- 05-03-2007 #1Just Joined!
- Join Date
- May 2007
- Posts
- 3
Bash Scripting
Help,
I need to write a script that prints the longest filename in a directory. The directory is supplied as an argument to the script. I have no problems supplying and checking the argument, but am a bit lost after that. I tried the following
for file in "$( find $1 -size +0)"
do
strlen=${#file}
if [ "$strlen" -gt "$filelen" ]; thne
filelen=$strlen
filename=$file
fi
done
echo "Filename is $filename"
can anyone help, in desperation
Drew
- 05-04-2007 #2Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
Hi,
This would do the job:
Check the man page for the test command and look through a couple of scripting tutorials:Code:#!/bin/sh if [ -z "$1" ] then echo "Usage: `basename $0` directory-name" exit 1 fi cd $1 let "maxlen=0" for line in * do strlen=`echo ${#line}` if [ -f "$line" ] then strlen=${#line} if [ $maxlen -lt $strlen ] then let "maxlen=strlen" longestfilename=$line fi fi done echo $longestfilename : $maxlen exit 0
http://www.tldp.org/LDP/Bash-Beginne...tml/index.html
http://tldp.org/LDP/abs/html/
Any doubts? Feel free to ask more questions.
And... your code would be easier to read if you placed it in CODE blocks!
Regards
- 05-06-2007 #3Just Joined!
- Join Date
- May 2007
- Posts
- 3
More Bash scripting
Hey all,
I need to find the starting character to a string provided as an argument to a script.
I thought that this would work:
firstch=`grep -e ^.{1} --label='hello'`
Im using the literal 'hello' to test the command
echo $firstch
I want firstch to equal 'h'
thanks in advance
Drew
- 05-06-2007 #4Just Joined!
- Join Date
- May 2007
- Posts
- 3
I sorted the previous one, I'm trying to check all the supplied arguments
while [ $currentarg -le $$numofargs ]; do
if [ ! -f ${$currentarg} ]; then
echo "File does not exist"
exit 1
else
( $currentarg ++ )
fi
done
What is wrong with the variable substitution?
if [ ! -f ${$currentarg} ]; then
- 05-06-2007 #5Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
Are you trying to test the parameters of your script? And are you expecting directory names as parameters? The -f operator returns true if the substituted variable is a regular file.
To test how variable substitution or whatever works, write a little script like this:
Use shift to read the next parameter within a loop, have a read of this:Code:#!/bin/sh currentarg=$1 if [ ! -f "$currentarg" ]; then echo $currentarg is not a regular file fi
http://www.tldp.org/LDP/Bash-Beginne...ect_09_07.html
If you want to really learn it, it is suggested that you learn by reading and experiencing it yourself.
And place questions about scripting in the "Linux Programming & Scripting" section.
Regards
- 05-07-2007 #6Linux User
- Join Date
- Jan 2007
- Location
- cleveland
- Posts
- 448
if "hello" is a file containing its own name, these work OK:
firstch=`cut -c 1 <hello`
firstch=`dd bs=1 count=1 2>/dev/null <hello`
firstch=`awk '{print substr($0,1,1)' <hello`the sun is new every day (heraclitus)


Reply With Quote
