LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 03-30-2006, 02:22 AM   #1
MikeAtVillage
Member
 
Registered: May 2005
Location: Mid Wales (UK)
Distribution: Slackware 8.0, Suse 10.0/1/2, CentOS (SMEServer7)
Posts: 38

Rep: Reputation: 15
Help with sh script syntax needed !


Hi folks, I'm having a bit of a problem ! Below is a short piece of script (mlist.sh) I'm using as a test - when I get the syntax sorted out it will be modified and used to copy files from one server to another. If I just type mlist.sh at the prompt the script lists all files in /home/mike OK but I want to use the $1 parameter to restrict listing to a particular pattern i.e. mlist.sh *.txt but when I try that I only get 1 filename reported (should be at least 10). Obviously I have a problem somewhere, anyone help ?

#!/bin/sh
#
#
#
cd /
cd /home/mike
thelist="$(ls $1)"
for fname in $thelist ; do
echo "File is $fname "
done
exit 0
 
Old 03-30-2006, 03:12 AM   #2
MikeAtVillage
Member
 
Registered: May 2005
Location: Mid Wales (UK)
Distribution: Slackware 8.0, Suse 10.0/1/2, CentOS (SMEServer7)
Posts: 38

Original Poster
Rep: Reputation: 15
Um, I've also just noticed that if the one file found has spaces in the name ( ie. Got It !.txt" output is:-

ls: Got: No such file or directory
ls: It: No such file or directory
ls: !.txt: No such file or directory

I must be doing something really wrong here !
 
Old 03-30-2006, 05:48 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,361

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
You'll need double quotes round the $fname to avoid probs with spaces in the name.
To avoid similar probs with (spaced) filenames as an input param, change the IFS (internal field separator) to just a newline eg:
IFS='
'
 
Old 03-30-2006, 08:08 PM   #4
mikshaw
LQ Addict
 
Registered: Dec 2003
Location: Maine, USA
Distribution: Slackware/SuSE/DSL
Posts: 1,320

Rep: Reputation: 45
Here's an alternative...

for fname in "$*"; do
echo "File is $fname "
done
 
Old 03-31-2006, 02:39 AM   #5
MikeAtVillage
Member
 
Registered: May 2005
Location: Mid Wales (UK)
Distribution: Slackware 8.0, Suse 10.0/1/2, CentOS (SMEServer7)
Posts: 38

Original Poster
Rep: Reputation: 15
Thanks Chris, changing IFS sorted out the problem of space in the filename. Strange though, the first time I ran it as mtest.sh *.txt it still only reported 1 file.... but I ran it again and it reported 5 files (which is the correct number)! :-)

I should be able to incorporate it into my full scripts now.

Thanks again
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic array of function pointers - syntax needed seriesx4 Programming 6 01-05-2006 07:45 AM
shell script help required syntax error sridhar11 Programming 1 10-31-2005 08:38 AM
BASH - simple script issue - syntax? tw1ggy5 Programming 11 05-21-2004 09:09 AM
help with basic syntax in bash script Supp0rtLinux Linux - Software 4 03-27-2003 06:57 PM
Shell Script Syntax dragon49 Programming 1 03-12-2003 09:25 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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