LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-28-2004, 11:02 AM   #1
tw1ggy5
LQ Newbie
 
Registered: Feb 2004
Posts: 2

Rep: Reputation: 0
BASH - simple script issue - syntax?


Am currently slowly going insane.
Have to write a simple bash script to take a dir as an argument, taking the current dir if one isnt given. Check the directory exists, then if it does list the directory properties.

if [ $# -ne 1 ]
then
echo You have not supplied a directory
echo Your current directory $(pwd) will be used
fi
if [ ! -e $1 ]
then
echo Directory does not exist
else
echo Searching $1
fi

Both of these if statements work correctly if used seperately, however when I put them as above then the first if works fine, but no output whatsoever from the second, regardless of input. What the hell am I doing wrong?

Even tried putting the second if as a seperate script then calling it from within this, but the same results, however when ran on it's own it works fine....

 
Old 02-28-2004, 11:40 AM   #2
tw1ggy5
LQ Newbie
 
Registered: Feb 2004
Posts: 2

Original Poster
Rep: Reputation: 0
Oh well fixed it anyway.

if [ ! -e $1 ]
then
echo no dir
else
if [ $# -ne 1 ]
then
echo using $(pwd)
else
echo using $1
fi
fi

Will have more problems later no doubt.
 
Old 03-02-2004, 05:24 PM   #3
jlarmour
LQ Newbie
 
Registered: Feb 2004
Location: Earth, early 21st century
Distribution: mandrake, slackware
Posts: 9

Rep: Reputation: 0
You can have a look at "man bash", look (WAY down) for the section on Parameter Expansion. This expression says "if $1 is not set use $PWD as a default, and put the result in $myDir":

myDir=${1:-$PWD}

NOTE: There are NO SPACES in this expression. Don't add any to make it more readable.
 
Old 03-11-2004, 09:14 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
don't do $(pwd), use $PWD

and [ -e ] tests a file exists, it may not be a directory
use [ -d ] file exists and is a directory
 
Old 05-10-2004, 11:12 AM   #5
xfiles_wolvie
Member
 
Registered: Mar 2004
Location: Singapore
Distribution: RH 9.0
Posts: 34

Rep: Reputation: 15
hi evey1

from wat we have here, i would like to ask a realted question.

after we take inthe dir as a argument, how do we list out the files in the dir n then compare the filenames and print out the file that has the longest filename?

i did for example

echo "pls enter pathname:'
read pathname

for files in $pathname
do
echo $pathname; ls -l
done

is there anithing wrong?
 
Old 05-11-2004, 07:46 PM   #6
jlarmour
LQ Newbie
 
Registered: Feb 2004
Location: Earth, early 21st century
Distribution: mandrake, slackware
Posts: 9

Rep: Reputation: 0
Quote:
Originally posted by xfiles_wolvie
after we take inthe dir as a argument, how do we list out the files in the dir n then compare the filenames and print out the file that has the longest filename?
If you want to do things with string lengths, there are much better tools to use than shell scripts. Perl or awk would be good choices:
awk:
ls | awk '{if(length($1) > longest)
{ longest = length($1); result = $1}}
END{print result}'
 
Old 05-12-2004, 07:40 AM   #7
xfiles_wolvie
Member
 
Registered: Mar 2004
Location: Singapore
Distribution: RH 9.0
Posts: 34

Rep: Reputation: 15
Hi jlarmour,

of cos i agree wif u. sed and awk is def better although im jus a beginner cos i saw a few scripts that can easily achieve wat i wan if i use awk n sed. bt before i go there, i would wan to explore wif bash 1st....thanks alot...keep them coming..regards
 
Old 05-16-2004, 04:26 AM   #8
xfiles_wolvie
Member
 
Registered: Mar 2004
Location: Singapore
Distribution: RH 9.0
Posts: 34

Rep: Reputation: 15
hi,

y is it that the command,

"ls -f $path"

doesnt work?as in it list my subdirectories in my directory ($path) together wif my normal files? isnt it supposed to list out only normal files?..regular files?then y is it listing out hte subdirectories in the given path?
 
Old 05-16-2004, 07:48 AM   #9
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,491

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
If you want to avoid awk or perl, and stick with shell, you can get the longer filename (excluding directories names) of the current directory with this script:

max=0
for i in $(ls)
do
l=$(expr $i : '.*')
if [ $l -gt $max -a ! -d $i ]
then
max=$l
r=$i
fi
done
echo $r : $max
 
Old 05-16-2004, 09:34 AM   #10
xfiles_wolvie
Member
 
Registered: Mar 2004
Location: Singapore
Distribution: RH 9.0
Posts: 34

Rep: Reputation: 15
hi jlliagre

thanks for he comparing..i have managed to get it earlier on. thanks aniway but mayb u can help me wif my prev qn.

is linux consider subdirectories as files when u use hte command

ls -f $path

becos for this command, after i run it, it shows me teh subdirectories and normal files in hte listing. how do i get them to show only the normal files and exclude the subdirectories?

also

if u r given a script that accepts a pathname as a argument, for eg, script named 'search' and is located in /home/user directory, when u key in the pathname /home/user as an argument to the script, it will run. but when u key in another pathname for eg. /home/user2, the script is nt able to run.
is it possible?

Last edited by xfiles_wolvie; 05-16-2004 at 09:37 AM.
 
Old 05-16-2004, 05:21 PM   #11
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,491

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
Not only with linux but under all unix variants, everything that has a path is considered a file, whether it is a directory, a character or block device, a symlink, a named pipe, or a plain file, and I forget probably a couple.

The script I suggested is already excluding the directories.

The "-f" ls option was never meant to display only files, see man ls for details.
 
Old 05-21-2004, 09:09 AM   #12
xfiles_wolvie
Member
 
Registered: Mar 2004
Location: Singapore
Distribution: RH 9.0
Posts: 34

Rep: Reputation: 15
thanks for all your help

Last edited by xfiles_wolvie; 05-23-2004 at 09:35 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple bash script Soulful93 Programming 1 06-04-2005 07:26 PM
Simple Bash Script Help njdownes Programming 2 03-05-2005 07:35 AM
Help with simple bash script - please tw001_tw Linux - Software 5 12-19-2004 09:02 PM
help with basic syntax in bash script Supp0rtLinux Linux - Software 4 03-27-2003 06:57 PM
Simple BASH script fio Linux - General 5 01-18-2003 03:26 PM


All times are GMT -5. The time now is 06:56 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration