user1356163
user1356163

Reputation: 407

cd not working as intended in bash script

Er.. There is a question with almost the same title on stackoverflow, but there is a difference in my question. My code is this

cd /oracle_data/indata/VXMLIVR
ls -d SMSR > temporary4.txt
while read smsr_line;do echo $smsr_line;cd $smsr_line;./smsr_alarm;cd ..;done < temporary4.txt
ls -d SMSC > temporary3.txt
while read smsc_line;do echo $smsc_line;cd $smsc_line;./smsc_alarm;cd ..;done < temporary3.txt
ls -d HCIVR5 > temporary.txt
while read hcivr_line;do echo $hcivr_line;cd $hcivr_line;./hcivr_alarm;cd ..;done < temporary.txt
ls -d HCIVR6 > temporary.txt
while read hcivr_line;do echo $hcivr_line;cd $hcivr_line;./hcivr_alarm;cd ..;done < temporary.txt
ls -d HCIVR7 > temporary.txt
while read hcivr_line;do echo $hcivr_line;cd $hcivr_line;./hcivr_alarm;cd ..;done < temporary.txt
ls -d HCIVR8 > temporary.txt
while read hcivr_line;do echo $hcivr_line;cd $hcivr_line;./hcivr_alarm;cd ..;done < temporary.txt
ls -d IVR* > temporary2.txt
while read ivr_line;do echo $ivr_line;cd $ivr_line;./ivr_alarm;cd ..;done < temporary2.txt
rm temp*

I have saved this script as vas_alarm. when i remove the first line(cd /oracle_data/indata/VXMLIVR), manually do cd /oracle_data/indata/VXMLIVR and run ./vas_alarms everything works fine. But when i run the entire script like this:

oracle@sunv440$/oracle_data/indata/VXMLIVR/vas_alarms
: No such file or directory/vas_alarms: line 1: cd: /oracle_data/indata/VXMLIVR

im getting an error. Where am i going wrong? The thing is my script is saved in /oracle_data/indata/VXMLIVR and i need to run it after entering this location. Also the script has to be saved in a cron file. Any workarounds will also do. My main aim is to schedule /oracle_data/indata/VXMLIVR/vas_alarms

Upvotes: 1

Views: 431

Answers (2)

tripleee
tripleee

Reputation: 189307

I have a few comments, but since the comment facility doesn't have support for code snippets, I'm posting this as an answer.

Your code could really use some refactoring. You should avoid repetitive code, and you should avoid temporary files. You should avoid parsing the output of ls. You are sometimes overwriting the temporary file, and sometimes not.

cd .. is somewhat brittle when symlinks are involved. A common idiom is to do cd in a subshell, which will cause the parent shell to remain in the current directory.

So here then is my attempt at refactoring;

#!/bin/sh

cd /oracle_data/indata/VMXLIVR

while read cmd dirs; do
    for dir in $dirs; do
        for subdir in $dir/*/.; do
            echo "$subdir"
            ( cd "$subdir"; ./"$cmd"; )
        done
    done
done <<HERE
    smsr_alarm  SMSR
    smsc_alarm  SMSC
    hcivr_alarm HCIVR[5678] 
    ivr_alarm   IVR*        
HERE

This will break if some of the subdirectories contain whitespace in their names. I am making some assumptions about the directory structure which are implied but not clearly demonstrated in your text.

Upvotes: 1

kev
kev

Reputation: 161604

This happens when you have some invisible characters in your file.

For example:

$ touch hello

$ echo $'\rhello'
hello

$ ls $'\rhello'
hello: No such file or directory

You can use xxd/hexdump/od to view the binary presentation:

$ echo $'\rhello' | xxd
0000000: 0d68 656c 6c6f 0a                        .hello.

xxd will print all unprintable charactors as .s(dots).
Notice that these's a .(dot) at the beginning.

Upvotes: 3

Related Questions