bash - cd command fails when directory is extracted from windows file -


i have 1 text file in windows contains lots of directories need extract. tried extract 1 directory , tried cd in shell script, cd command failed, prompting cd: /var/gpio/: no such file or directory.

i have confirmed directory exists in local pc , directory correct (though relative). have searched lot, seems special windows characters exist in extract file. tried see them cat -a check , result ^[[m^[[k^[[m^[[kvar/gpio/$

i don't know meaning of m^ or [[k. please me problem? use cygwin in windows 7 64-bit. below related code review:

templt_dir=$(cat temp | grep -m 1 "$templt_name" |head -1 | sed -n "s#$templt_name##p" | sed -n "s#\".*##p") echo $templt_dir     ###comment, runs output: /var/gpio/, that's correct! cd $templt_dir       ###comment, cd error prompts cat temp | grep -m 1 "$templt_name" |head -1 | sed -n "s#$templt_name##p" | sed -n "s#\".*##p" > check  ###comment, problem checking 

below content of check file:

$ cat -a check ^[[m^[[k^[[m^[[kvar/gpio/$ 

to confirm directory correct, below results of ls -l on /var:

$ ls var -l total 80k drwxrwx---+ 1 administrators domain users 0 jun 24 11:11 analog/ drwxrwx---+ 1 administrators domain users 0 jun 24 11:37 communication/ drwxrwx---+ 1 administrators domain users 0 jun 24 11:10 gpio/ drwxrwx---+ 1 administrators domain users 0 jun 24 11:11 humaninterface/ drwxrwx---+ 1 administrators domain users 0 jun 24 11:11 memory/ drwxrwx---+ 1 administrators domain users 0 jun 24 11:11 pwm/ drwxrwx---+ 1 administrators domain users 0 jun 24 11:10 security/ drwxrwx---+ 1 administrators domain users 0 jun 24 11:11 system/ drwxrwx---+ 1 administrators domain users 0 jun 25 16:25 timers/ drwxrwx---+ 1 administrators domain users 0 jun 24 11:10 universaldevice/ 

the error message cd: /var/gpio/: no such file or directory indicates name stored in $templt_dir doesn’t exist.

this due string containing non-printing ansi escape sequences. need remove these characters string containing directory.

i found following sed substitution this unix , linux answer

sed -r "s/\x1b\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|k]//g" 

which should include in pipe command:

templt_dir=$(grep -m 1 "$templt_name" temp | sed -n "s#$templt_name##p; s#\".*##p" | sed -r "s/\x1b\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|k]//g") 

note: concatenated 2 sed substitutions 1 command , removed unnecessary cat. removed redundant head -1 since grep -m 1 should output 1 line. can combine sed substitutions one: sed -r "s#$templt_name##; s#\".*##; s/\x1b\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|k]//g" (the -n sed option , p sed command can left out if there’s line being processed can’t test without having original file).

other ways of using sed strip ansi escape sequences listed @ remove color codes (special characters) sed.

however, better long-term fix modify process creates text file listing directories not include ansi escape codes in output.


Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -