perform file actions located in another direcory

Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2009
Posts: 9
Reputation: karpaklu is an unknown quantity at this point 
Solved Threads: 0
karpaklu karpaklu is offline Offline
Newbie Poster

perform file actions located in another direcory

 
0
  #1
Oct 11th, 2009
hai dani,

i have a problem with listing the contents of file in another directory.

my default path is in /home/pallu
where my file called DirProg script exists , now i want to find any file which starts with a or A in a dir and store the content in another file called output.dat.

im not able to dispaly file in the directory threw script.

my code is below:----


echo "Enter a directory name"
read dname

if test -d $dname
then

if grep -c ^A dirlist1
then
fname=`grep ^A dirlist1`
cat $fname|cat > output.dat
echo "Content of file is written in a file called output.dat"
fi



else
echo "The entered name is not a directory"
fi



thanks in advance.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,258
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 579
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #2
Oct 11th, 2009
You should probably use find :
Shell Scripting Syntax (Toggle Plain Text)
  1. #!/bin/bash
  2. echo -n "Enter a directory: "
  3. read dir
  4.  
  5. if ! test -d ${dir}; then
  6. echo "Directory does not exist"
  7. exit
  8. fi
  9.  
  10. # To search child directories
  11. rm -f output_withchildren.dat
  12. find ${dir} -type f -iname a\* >> output_withchildren.dat
  13.  
  14. # To not search child directories
  15. rm -f output_nochildren.dat
  16. find ${dir} -maxdepth 1 -type f -iname a\* >> output_nochildren.dat
Last edited by sknake; Oct 11th, 2009 at 8:02 am.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 64
Reputation: cfajohnson is an unknown quantity at this point 
Solved Threads: 13
cfajohnson cfajohnson is offline Offline
Junior Poster in Training
 
0
  #3
Oct 19th, 2009
Originally Posted by karpaklu View Post
hai dani,

i have a problem with listing the contents of file in another directory.

my default path is in /home/pallu
where my file called DirProg script exists , now i want to find any file which starts with a or A in a dir and store the content in another file called output.dat.

im not able to dispaly file in the directory threw script.

my code is below:----

Please use CODE tags.

Shell Scripting Syntax (Toggle Plain Text)
  1. echo "Enter a directory name"
  2. read dname
  3.  
  4. if test -d $dname
  5. then
  6.  
  7. if grep -c ^A dirlist1
  8. then
  9. fname=`grep ^A dirlist1`
  10. cat $fname|cat > output.dat

Why two cats?

Shell Scripting Syntax (Toggle Plain Text)
  1. cat $fname > output.dat
Shell Scripting Syntax (Toggle Plain Text)
  1. echo "Content of file is written in a file called output.dat"
  2. fi
  3.  
  4.  
  5.  
  6. else
  7. echo "The entered name is not a directory"
  8. fi

Shell Scripting Syntax (Toggle Plain Text)
  1. read dname
  2. if [ -d "$dname" ]
  3. then
  4. printf "%s\n" "$dname"/A* > output.dat
  5. else
  6. echo "The entered name is not a directory"
  7. fi
Chris F.A. Johnson
http://cfajohnson.com
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC