The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
question about testing in shell programming thungmail Shell Programming and Scripting 6 04-09-2008 02:04 PM
question about testing in shell programming(modifications were made) thungmail Shell Programming and Scripting 2 04-08-2008 03:51 PM
question about testing in shell programming thungmail Shell Programming and Scripting 2 04-08-2008 02:29 AM
question about testing in shell programming thungmail Shell Programming and Scripting 3 04-02-2008 10:46 PM
question about about Shell programming thungmail Shell Programming and Scripting 1 04-01-2008 01:40 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-04-2008
thungmail thungmail is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 11
Post question about testing in shell programming

Hi i would like to write a "script" which takes a directory as an argument and the script will output the content of a file in this directory.Here is my code
Code:
#!/bin/sh
#set an argument to be a specified path
$1=/home/tuan/Desktop/Shell_programming/directory

#check if an argument is a directory
if [ ! -d $1 ]
then
	for number in $1
		do 
		echo "$number"
	done<list
else
	echo "No such that directory "
fi
After that in terminal I did:
Code:
tuan@tuan:~/Desktop/Shell_programming$ ./A3 directory
./A3: 2: directory=/home/tuan/Desktop/Shell_programming/directory: not found
No such that directory 
tuan@tuan:~/Desktop/Shell_programming$
It seems there are mistakes. Can anyone point it out for me.
PS: In "directory" directory, I did created a file named "list" like below
Code:
1
2
3
  #2 (permalink)  
Old 04-04-2008
Juha Juha is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 36
Try to use something else than "$1" as the variable name... e.g.
Code:
#!/bin/sh
#set an argument to be a specified path
a=/home/tuan/Desktop/Shell_programming/directory

#check if an argument is a directory
if [ -d $a ]
then
	for number in $a
		do 
		echo "$number"
	done
else
	echo "No such that directory "
fi
  #3 (permalink)  
Old 04-04-2008
thungmail thungmail is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 11
here is the new one (with modification)
Code:
#!/bin/sh
#set an argument to be a specified path
a=/home/tuan/Desktop/Shell_programming/directory/list
#check if an argument is a directory
if [ ! -d $a ]
then
	for number in $a
		do 
		echo "$number"
	done
else
	echo "No such that directory "
fi
However, the output is
Code:
tuan@tuan:~/Desktop/Shell_programming$ ./A3
/home/tuan/Desktop/Shell_programming/directory/list
tuan@tuan:~/Desktop/Shell_programming$
I have no idea.......
  #4 (permalink)  
Old 04-05-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
The way to loop over files in a directory is to list them.

Code:
for number in "$a"/*; do ...
If you want to constrain this to just files whose names are a number, use a different wildcard. This loops over all files in the selected directory.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:47 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0