I have the following script:
#!/bin/bash ls -1 | while read d do [[ -f "$d" ]] && continue echo $d cd $d done
The problem is that each cd says "[path]: there is no such file or directory", why? The folder exists because I list it ...
I see two problems in your code:
cd
Please try the following:
#!/bin/bash ls -1 | while read d do test -d "$d" || continue echo $d (cd $d ; echo "In ${PWD}") done
You should not use ls.
ls
#!/bin/bash for d in * do [[ ! -d "$d" ]] && continue echo "$d" cd "$d" # do something cd "$OLDPWD" done
, script. , cd , cd.
dir=`pwd` ls -1 | while read d do [[ -f "$d" ]] && continue cd "$dir" echo $d cd $d #.... done
, . - ?
:
#!/bin/bash CWD="$(pwd)" #save starting directory ls -1 | while read d do [[ ! -d "$d" ]] && continue cd "$d" echo "Changed to directory" $d #Now you can do someting in the sub-directory cd "$CWD" #Change back to old directory done