For loop failing cd command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting For loop failing cd command
# 1  
Old 10-26-2012
For loop failing cd command

Hi guys,

i've wrote the following loop;


Code:
for i in `ls`
 do
 cd $i/host
 cat "xxxx.txt" |grep "yyyy" >> zzzz.txt
 done

I have a set of folder with different name and i need to extract a value from a file contained in the host subfolder ( that is present in each folder).

When i run the script it returns the following error
"No such file or directory", but actually it exists...
# 2  
Old 10-26-2012
You are changing into directories and never changing back out. folder2 may exist, but it probably doesn't inside folder1! So do cd ../../ at the bottom of the loop to back back out.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 10-26-2012
Try this:-

Code:
for i in `find . -type d`
do
   cd ${i}/host
   cat xxxx.txt |grep "yyyy" >> zzzz.txt
   cd -
done

This User Gave Thanks to Yoda For This Post:
# 4  
Old 10-27-2012
MySQL

Thanks guys!!

@Image bipinajith

Find command is recursive so it's better to use command such as 'ls'.

There was a missing / after
Code:
cd $i/host

Now i'm trying to understand the reason why the output redirection is not working...

Last edited by cecco16; 10-27-2012 at 07:46 AM..
# 5  
Old 10-27-2012
You can also use complete folder structure
Code:
cd /folder/${i}/host

then you no need to return to previous folder
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to auto correct a failing command?

If a command is not found, e.g. nawk, this is how I fix the problem ] && NAWK=/usr/bin/gawk ] && NAWK=/usr/bin/nawk ] && NAWK=/usr/bin/awkI use $NAWK an the set the appropriate value based on the system it runs. How can I implement a similar fix for a command found but illegal argument.... (6 Replies)
Discussion started by: mohtashims
6 Replies

2. UNIX for Advanced & Expert Users

While trying to load .so file manually using command its failing

Hi all, I am newbie to linux environment. I was trying to run an .so file manually which in turn call a method in bin folder. Command given, XXX_MODULES=libxxx.so /opt/servicename/bin/methodname -Le -c /opt/servicename/etc/methodname/methodname.conf -n -C -t -m "" When i tried to execute... (1 Reply)
Discussion started by: sharathpadman
1 Replies

3. UNIX for Dummies Questions & Answers

While trying to load .so file manually using command its failing

Hi all, I am newbie to linux environment. I was trying to run an .so file manually which in turn call a method in bin folder. Command given, XXX_MODULES=libxxx.so /opt/servicename/bin/methodname -Le -c /opt/servicename/etc/methodname/methodname.conf -n -C -t -m "" When i tried to... (1 Reply)
Discussion started by: sharathpadman
1 Replies

4. Shell Programming and Scripting

For loop with lftp command

Hi Experts, I am using the below code for lftp with for loop but getting error at line 18. Not able to understand if am using the for loop wrongly. #!/bin/bash FILE='/home/user/d.txt' FILEName=/home/user/Test/HostName.txt for HOST in $(awk '{ print $0}' $FILEName);do ... (5 Replies)
Discussion started by: sharsour
5 Replies

5. Shell Programming and Scripting

Connect (SSH) to Windows server via Linux server through a script and passing command.. but failing

I am trying to connect to Windows server via Linux server through a script and run two commands " cd and ls " But its giving me error saying " could not start the program" followed by the command name i specify e g : "cd" i am trying in this manner " ssh username@servername "cd... (5 Replies)
Discussion started by: sunil seelam
5 Replies

6. Shell Programming and Scripting

Do While Loop Command help

Hello, Can some one help me with do-while loop command and writing the out put to a file. Below is the command I want to use. Command: netstat -an |grep 1421 |grep "ESTABLISHED" |wc -l thanks in advance. (11 Replies)
Discussion started by: skrv
11 Replies

7. Shell Programming and Scripting

Script for telnet and run one command kill it and run another command using while loop

( sleep 3 echo ${LOGIN} sleep 2 echo ${PSWD} sleep 2 while read line do echo "$line" PID=$? sleep 2 kill -9 $PID done < temp sleep 5 echo "exit" ) | telnet ${HOST} while is executing only command and exits. (5 Replies)
Discussion started by: sooda
5 Replies

8. Shell Programming and Scripting

tty command failing

We have script like this in the .bash_profile.. #-# determine if session is interactive or in background if ]; then while true; do read -p "Do you wish to load profile yes or no?" yn case $yn in * ) source /opt/oracle/.profile; break;; * ) break;; *... (2 Replies)
Discussion started by: talashil
2 Replies

9. Shell Programming and Scripting

Need a Command To display "echo command value in loop" in single line.

Hi I want to display "echo command value in loop" in single line. My requirement is to show the input file (test_1.txt) like the output file (test_2.txt) given below. Input file :test_1.txt a1|b1|4|5 a1|b1|42|9 a2|b2|32|25 a1|b1|2|5 a3|b3|4|8 a2|b2|14|6 Output file:test_2.txt... (2 Replies)
Discussion started by: sakthifire
2 Replies

10. HP-UX

dd command failing

I am new to HP-UX. I have an 8GB drive that is my root drive, contained in a Volume Group. I would like to clone that drive to another drive, which is 18.4GB. The other drive is not in a volume group. I am using this simple command:# dd if=/dev/dsk/c0t6d0 of=/dev/dsk/c0t5d0The command... (4 Replies)
Discussion started by: emsecrist
4 Replies
Login or Register to Ask a Question