How would I run a powershell script that would run on either, the exit command, or the shell closing. I would want this script to run on every shell close not just for one shell.
Script
Using Register-EngineEvent you can do this:
Register-EngineEvent PowerShell.Exiting –Action { 'Type your Code' }
# Alternatively with hiding the event:
Register-EngineEvent PowerShell.Exiting -SupportEvent –Action { yourExitFunction; }
Get it out of the function and run it in the scriptblock, like this:
Register-EngineEvent PowerShell.Exiting –Action {
$foo = #"
.d8888b. 8888888888 8888888888 Y88b d88P .d88888b. 888 888
d88P Y88b 888 888 Y88b d88P d88P" "Y88b 888 888
"Y888b. 8888888 8888888 Y888P 888 888 888 888
"Y88b. 888 888 888 888 888 888 888
"888 888 888 888 888 888 888 888
Y88b d88P 888 888 888 Y88b. d88P Y88b. .d88P
"Y8888P" 8888888888 8888888888 888 "Y88888P" "Y88888P"
.d8888b. 8888888b. d8888 .d8888b. 8888888888
d88p Y88b 888 Y88b d88888 d88P Y88b 888
"Y888b. 888 d88P d88P 888 888 8888888
"Y88b. 8888888P" d88P 888 888 888
"888 888 d88P 888 888 888 888
Y88b d88P 888 d8888888888 Y88b d88P 888
"Y8888P" 888 d88P 888 "Y8888P" 8888888888
.d8888b. .d88888b. 888 888 888888b. .d88888b. Y88b d88P
d88P Y88b d88P" "Y88b 888 o 888 888 "88b d88P" "Y88b Y88b d88P
888 888 888 888 d888b 888 8888888K. 888 888 Y888P
888 888 888 8888888888888 888 "Y88b 888 888 888
888 888 888 888 88888P Y88888 888 888 888 888 888
Y88b d88P Y88b. .d88P 8888P Y8888 888 d88P Y88b. .d88P 888
"Y8888P" "Y88888P" 888P Y888 8888888P" "Y88888P" 888
"#
$file="c:\.seeyouspacecowboy"
$foo | out-file $file
$colour=("red","yellow","darkyellow","green","cyan","darkcyan","darkmagenta")
[int]$num=-1
$info = (get-content $file)
Write-Host ""
foreach($i in $info)
{
[int]$perspective=($num / 3)
write-host $i -foregroundcolor $colour[$perspective]
$num++
}
Write-Host ""
}
*of course, make sure you are with admin privilege as it write to c:\
Another option is a try-finally block, which will execute the finally block on error or ctrl-c:
try {
# Main code
} finally {
# Code to run on exit
}
Related
I have recently come across SurrealDB, and installed it.
After adding to path, I started the server using surreal start --log trace --user root --pass root
.d8888b. 888 8888888b. 888888b.
d88P Y88b 888 888 'Y88b 888 '88b
Y88b. 888 888 888 888 .88P
'Y888b. 888 888 888d888 888d888 .d88b. 8888b. 888 888 888 8888888K.
'Y88b. 888 888 888P' 888P' d8P Y8b '88b 888 888 888 888 'Y88b
'888 888 888 888 888 88888888 .d888888 888 888 888 888 888
Y88b d88P Y88b 888 888 888 Y8b. 888 888 888 888 .d88P 888 d88P
'Y8888P' 'Y88888 888 888 'Y8888 'Y888888 888 8888888P' 8888888P'
[2022-09-27 17:37:44] INFO surrealdb::iam Root authentication is enabled
[2022-09-27 17:37:44] INFO surrealdb::iam Root username is 'root'
[2022-09-27 17:37:44] INFO surrealdb::dbs Database strict mode is disabled
[2022-09-27 17:37:44] INFO surrealdb::kvs Starting kvs store in memory
[2022-09-27 17:37:44] INFO surrealdb::kvs Started kvs store in memory
[2022-09-27 17:37:44] INFO surrealdb::net Starting web server on 0.0.0.0:8000
[2022-09-27 17:37:44] INFO surrealdb::net Started web server on 0.0.0.0:8000
In my NodeJS app, I have the following code (adapted from their docs):
import Surreal from 'surrealdb.js';
const db = new Surreal('http://localhost:8000/rpc');
async function main() {
try {
// Signin as a namespace, database, or root user
await db.signin({
user: 'root',
pass: 'root',
NS: 'practice',
DB: 'buybig'
});
console.log('y');
// Select a specific namespace / database
// await db.use('practice', 'buybig');
console.log(await db.select('users'));
} catch (e) {
console.error('ERROR', e);
}
}
main();
I am getting this error:
ERROR AuthenticationError: There was a problem with authentication
at Surreal._Surreal_signin (file:///C:/Users/jaide/OneDrive/Documents/Projects/HTMLProject/buybig/node_modules/surrealdb.js/esm/index.js:416:23) at Surreal.<anonymous> (file:///C:/Users/jaide/OneDrive/Documents/Projects/HTMLProject/buybig/node_modules/surrealdb.js/esm/index.js:225:111)
at Surreal.f (file:///C:/Users/jaide/OneDrive/Documents/Projects/HTMLProject/buybig/node_modules/surrealdb.js/esm/classes/emitter.js:28:18)
at file:///C:/Users/jaide/OneDrive/Documents/Projects/HTMLProject/buybig/node_modules/surrealdb.js/esm/classes/emitter.js:34:22
at Array.forEach (<anonymous>)
at Surreal.emit (file:///C:/Users/jaide/OneDrive/Documents/Projects/HTMLProject/buybig/node_modules/surrealdb.js/esm/classes/emitter.js:33:67)
at Socket.<anonymous> (file:///C:/Users/jaide/OneDrive/Documents/Projects/HTMLProject/buybig/node_modules/surrealdb.js/esm/index.js:126:29)
at file:///C:/Users/jaide/OneDrive/Documents/Projects/HTMLProject/buybig/node_modules/surrealdb.js/esm/classes/emitter.js:34:22
at Array.forEach (<anonymous>)
at Socket.emit (file:///C:/Users/jaide/OneDrive/Documents/Projects/HTMLProject/buybig/node_modules/surrealdb.js/esm/classes/emitter.js:33:67)
^C
And this in my SurrealDB logs:
[2022-09-27 18:06:04] INFO surreal::web 127.0.0.1:64675 GET /rpc HTTP/1.1 101 "-" 68.7µs
Accessing database through VSCode's Thunder Client and SurrealDB cli tool works flawlessly.
Any help is appreciated.
I tried that as well and got the same error. The following worked for me:
let dataBase = await new Surreal();
await dataBase.connect("http://127.0.0.1:8000/rpc");
await dataBase.signin({
user: "root",
pass: "root",
});
await dataBase.use("test", "test");
let result = await dataBase.create("user:someone", {
name: { first: "someone", last: "else" },
});
console.log(result);
I am working with the log filles arranged in the following format:
fƒdfFinding intramodel H-bonds
Constraints relaxed by 0.5 angstroms and 20 degrees
Models used:
1.1 SarsCov2_structure49R_nsp5holo_rep1.pdb
1.2 SarsCov2_structure49R_nsp5holo_rep1.pdb
1.3 SarsCov2_structure49R_nsp5holo_rep1.pdb
1.4 SarsCov2_structure49R_nsp5holo_rep1.pdb
1.5 SarsCov2_structure49R_nsp5holo_rep1.pdb
1.6 SarsCov2_structure49R_nsp5holo_rep1.pdb
1.7 SarsCov2_structure49R_nsp5holo_rep1.pdb
1.8 SarsCov2_structure49R_nsp5holo_rep1.pdb
1.9 SarsCov2_structure49R_nsp5holo_rep1.pdb
1.10 SarsCov2_structure49R_nsp5holo_rep1.pdb
1.11 SarsCov2_structure49R_nsp5holo_rep1.pdb
1.12 SarsCov2_structure49R_nsp5holo_rep1.pdb
1.13 SarsCov2_structure49R_nsp5holo_rep1.pdb
1.14 SarsCov2_structure49R_nsp5holo_rep1.pdb
14 H-bonds
H-bonds (donor, acceptor, hydrogen, D..A dist, D-H..A dist):
SarsCov2_structure49R_nsp5holo_rep1.pdb #1.1/? ASN 142 ND2 SarsCov2_structure49R_nsp5holo_rep1.pdb #1.1/A UNL 888 O SarsCov2_structure49R_nsp5holo_rep1.pdb #1.1/? ASN 142 1HD2 3.102 2.145
SarsCov2_structure49R_nsp5holo_rep1.pdb #1.3/? GLU 166 N SarsCov2_structure49R_nsp5holo_rep1.pdb #1.3/A UNL 888 O SarsCov2_structure49R_nsp5holo_rep1.pdb #1.3/? GLU 166 H 3.011 2.024
SarsCov2_structure49R_nsp5holo_rep1.pdb #1.4/? GLU 166 N SarsCov2_structure49R_nsp5holo_rep1.pdb #1.4/A UNL 888 O SarsCov2_structure49R_nsp5holo_rep1.pdb #1.4/? GLU 166 H 3.037 2.132
SarsCov2_structure49R_nsp5holo_rep1.pdb #1.5/? HIS 163 NE2 SarsCov2_structure49R_nsp5holo_rep1.pdb #1.5/A UNL 888 O no hydrogen 3.388 N/A
SarsCov2_structure49R_nsp5holo_rep1.pdb #1.5/? GLU 166 N SarsCov2_structure49R_nsp5holo_rep1.pdb #1.5/A UNL 888 O SarsCov2_structure49R_nsp5holo_rep1.pdb #1.5/? GLU 166 H 2.806 1.792
SarsCov2_structure49R_nsp5holo_rep1.pdb #1.7/? THR 26 N SarsCov2_structure49R_nsp5holo_rep1.pdb #1.7/A UNL 888 O SarsCov2_structure49R_nsp5holo_rep1.pdb #1.7/? THR 26 H 3.093 2.142
SarsCov2_structure49R_nsp5holo_rep1.pdb #1.7/? GLY 143 N SarsCov2_structure49R_nsp5holo_rep1.pdb #1.7/A UNL 888 O SarsCov2_structure49R_nsp5holo_rep1.pdb #1.7/? GLY 143 H 3.030 2.193
SarsCov2_structure49R_nsp5holo_rep1.pdb #1.9/? GLN 189 NE2 SarsCov2_structure49R_nsp5holo_rep1.pdb #1.9/A UNL 888 O SarsCov2_structure49R_nsp5holo_rep1.pdb #1.9/? GLN 189 2HE2 3.052 2.301
SarsCov2_structure49R_nsp5holo_rep1.pdb #1.10/? GLU 166 N SarsCov2_structure49R_nsp5holo_rep1.pdb #1.10/A UNL 888 O SarsCov2_structure49R_nsp5holo_rep1.pdb #1.10/? GLU 166 H 2.854 1.868
SarsCov2_structure49R_nsp5holo_rep1.pdb #1.12/? GLY 143 N SarsCov2_structure49R_nsp5holo_rep1.pdb #1.12/A UNL 888 O SarsCov2_structure49R_nsp5holo_rep1.pdb #1.12/? GLY 143 H 3.103 2.070
SarsCov2_structure49R_nsp5holo_rep1.pdb #1.13/? GLY 143 N SarsCov2_structure49R_nsp5holo_rep1.pdb #1.13/A UNL 888 O SarsCov2_structure49R_nsp5holo_rep1.pdb #1.13/? GLY 143 H 3.161 2.224
SarsCov2_structure49R_nsp5holo_rep1.pdb #1.13/? CYS 145 SG SarsCov2_structure49R_nsp5holo_rep1.pdb #1.13/A UNL 888 O SarsCov2_structure49R_nsp5holo_rep1.pdb #1.13/? CYS 145 HG 3.421 2.842
SarsCov2_structure49R_nsp5holo_rep1.pdb #1.14/? ASN 142 ND2 SarsCov2_structure49R_nsp5holo_rep1.pdb #1.14/A UNL 888 O SarsCov2_structure49R_nsp5holo_rep1.pdb #1.14/? ASN 142 2HD2 3.055 2.465
SarsCov2_structure49R_nsp5holo_rep1.pdb #1.14/? CYS 145 N SarsCov2_structure49R_nsp5holo_rep1.pdb #1.14/A UNL 888 O SarsCov2_structure49R_nsp5holo_rep1.pdb #1.14/? CYS 145 H 2.924 2.143
I need to find the first occurence of the "GLU 166 N" pattern and print the number present on the same line just before the pattern as #1.number/?, associated with this pattern. So in the example the detected number should be 3 (since the associating number is #1.3/?).
I would start from basic pattern-detection
awk '/GLU 166 N/' file
but how to find correctly the number defined just before the pattern and print it as output ? Finally, in the case if the pattern can not be found, I would like that the script prints 1.
$ awk -vn=1 '/GLU 166 N/ {gsub(/.*\.|\/\?/,"",$2); n=$2; exit} END {print n}' file
3
$ awk -vn=1 '/GLU 166 N/ {gsub(/.*\.|\/\?/,"",$2); n=$2; exit} END {print n}' /dev/null
1
What you look for is in the second field ($2). gsub(/.*\.|\/\?/,"",$2) replaces in $2 all leading characters up to (and including) the period, and the trailing /? by the empty string.
Using GNU awk for the 3rd arg to match():
$ awk 'match($0,/([0-9]+).. GLU 166 N /,a){print a[1]; exit}' file
3
or using any awk:
$ awk 'match($0,/[0-9]+.. GLU 166 N /){sub("/.*",""); print substr($0,RSTART); exit}' file
3
$ awk 'match($0,/[0-9]+.. GLU 166 N /){print substr($0,RSTART,RLENGTH-13); exit}' file
3
If GNU awk which supports gensub function is available, would you please try:
awk '/GLU 166 N/ {
print gensub(/^.*#1\.([0-9]+)\/\? GLU 166 N.*$/, "\\1", 1)
exit
}' file
The regex ^.*#1\\.([0-9]+)/\\? GLU 166 N.*$ matches the line with the substring #1.<number>/? "GLU 166 N. The <number> portion, which is enclosed with the parentheses in the regex as ([0-9]+) is captured as group 1, then the entire line is replaced with the group 1, which is specified as the replacement \\1, then it is printed as the result.
Alternatively you can say with GNU sed as:
sed -nE '0,/GLU 166 N/s|^.*#1\.([0-9]+)/\? GLU 166 N.*|\1|p' file
The address 0,/pattern/, where 0 is specific to GNU sed as a starting line, makes the script exit immediately after the 1st pattern match.
If awk is not requirement, you can use grep and cut. Simple is good.
λ cat input.txt
fƒdfFinding intramodel H-bonds
Constraints relaxed by 0.5 angstroms and 20 degrees
Models used:
1.1 SarsCov2_structure49R_nsp5holo_rep1.pdb
1.2 SarsCov2_structure49R_nsp5holo_rep1.pdb
1.3 SarsCov2_structure49R_nsp5holo_rep1.pdb
1.4 SarsCov2_structure49R_nsp5holo_rep1.pdb
1.5 SarsCov2_structure49R_nsp5holo_rep1.pdb
1.6 SarsCov2_structure49R_nsp5holo_rep1.pdb
1.7 SarsCov2_structure49R_nsp5holo_rep1.pdb
1.8 SarsCov2_structure49R_nsp5holo_rep1.pdb
1.9 SarsCov2_structure49R_nsp5holo_rep1.pdb
1.10 SarsCov2_structure49R_nsp5holo_rep1.pdb
1.11 SarsCov2_structure49R_nsp5holo_rep1.pdb
1.12 SarsCov2_structure49R_nsp5holo_rep1.pdb
1.13 SarsCov2_structure49R_nsp5holo_rep1.pdb
1.14 SarsCov2_structure49R_nsp5holo_rep1.pdb
14 H-bonds
H-bonds (donor, acceptor, hydrogen, D..A dist, D-H..A dist):
SarsCov2_structure49R_nsp5holo_rep1.pdb #1.1/? ASN 142 ND2 SarsCov2_structure49R_nsp5holo_rep1.pdb #1.1/A UNL 888 O SarsCov2_structure49R_nsp5holo_rep1.pdb #1.1/? ASN 142 1HD2 3.102 2.145
SarsCov2_structure49R_nsp5holo_rep1.pdb #1.3/? GLU 166 N SarsCov2_structure49R_nsp5holo_rep1.pdb #1.3/A UNL 888 O SarsCov2_structure49R_nsp5holo_rep1.pdb #1.3/? GLU 166 H 3.011 2.024
SarsCov2_structure49R_nsp5holo_rep1.pdb #1.4/? GLU 166 N SarsCov2_structure49R_nsp5holo_rep1.pdb #1.4/A UNL 888 O SarsCov2_structure49R_nsp5holo_rep1.pdb #1.4/? GLU 166 H 3.037 2.132
SarsCov2_structure49R_nsp5holo_rep1.pdb #1.5/? HIS 163 NE2 SarsCov2_structure49R_nsp5holo_rep1.pdb #1.5/A UNL 888 O no hydrogen 3.388 N/A
SarsCov2_structure49R_nsp5holo_rep1.pdb #1.5/? GLU 166 N SarsCov2_structure49R_nsp5holo_rep1.pdb #1.5/A UNL 888 O SarsCov2_structure49R_nsp5holo_rep1.pdb #1.5/? GLU 166 H 2.806 1.792
SarsCov2_structure49R_nsp5holo_rep1.pdb #1.7/? THR 26 N SarsCov2_structure49R_nsp5holo_rep1.pdb #1.7/A UNL 888 O SarsCov2_structure49R_nsp5holo_rep1.pdb #1.7/? THR 26 H 3.093 2.142
SarsCov2_structure49R_nsp5holo_rep1.pdb #1.7/? GLY 143 N SarsCov2_structure49R_nsp5holo_rep1.pdb #1.7/A UNL 888 O SarsCov2_structure49R_nsp5holo_rep1.pdb #1.7/? GLY 143 H 3.030 2.193
SarsCov2_structure49R_nsp5holo_rep1.pdb #1.9/? GLN 189 NE2 SarsCov2_structure49R_nsp5holo_rep1.pdb #1.9/A UNL 888 O SarsCov2_structure49R_nsp5holo_rep1.pdb #1.9/? GLN 189 2HE2 3.052 2.301
SarsCov2_structure49R_nsp5holo_rep1.pdb #1.10/? GLU 166 N SarsCov2_structure49R_nsp5holo_rep1.pdb #1.10/A UNL 888 O SarsCov2_structure49R_nsp5holo_rep1.pdb #1.10/? GLU 166 H 2.854 1.868
SarsCov2_structure49R_nsp5holo_rep1.pdb #1.12/? GLY 143 N SarsCov2_structure49R_nsp5holo_rep1.pdb #1.12/A UNL 888 O SarsCov2_structure49R_nsp5holo_rep1.pdb #1.12/? GLY 143 H 3.103 2.070
SarsCov2_structure49R_nsp5holo_rep1.pdb #1.13/? GLY 143 N SarsCov2_structure49R_nsp5holo_rep1.pdb #1.13/A UNL 888 O SarsCov2_structure49R_nsp5holo_rep1.pdb #1.13/? GLY 143 H 3.161 2.224
SarsCov2_structure49R_nsp5holo_rep1.pdb #1.13/? CYS 145 SG SarsCov2_structure49R_nsp5holo_rep1.pdb #1.13/A UNL 888 O SarsCov2_structure49R_nsp5holo_rep1.pdb #1.13/? CYS 145 HG 3.421 2.842
SarsCov2_structure49R_nsp5holo_rep1.pdb #1.14/? ASN 142 ND2 SarsCov2_structure49R_nsp5holo_rep1.pdb #1.14/A UNL 888 O SarsCov2_structure49R_nsp5holo_rep1.pdb #1.14/? ASN 142 2HD2 3.055 2.465
SarsCov2_structure49R_nsp5holo_rep1.pdb #1.14/? CYS 145 N SarsCov2_structure49R_nsp5holo_rep1.pdb #1.14/A UNL 888 O SarsCov2_structure49R_nsp5holo_rep1.pdb #1.14/? CYS 145 H 2.924 2.143
grep -om1 '[[:digit:]]*/? GLU 166 N' input.txt | cut -d/ -f1
3
To print 1 when the pattern is not found:
{ grep -om1 '[[:digit:]]*/? GLU 166 N' input.txt || echo 1; } | cut -d/ -f1
I'm trying to run a compiler but I'm getting an error saying it can not be found, but it looks to exist and the path is good. I even tried a different shell incase zsh was mis-configured, but got the same error. Lost at what to do, any suggestions?
6909077c228a% ls -l toolchain/bin/armv7l-timesys-linux-gnueabi-gcc
-rwxr-xr-x 2 root root 2287465 Sep 11 13:19 toolchain/bin/armv7l-timesys-linux-gnueabi-gcc
6909077c228a% ./toolchain/bin/armv7l-timesys-linux-gnueabi-gcc
zsh: no such file or directory: ./toolchain/bin/armv7l-timesys-linux-gnueabi-gcc
#switch to bash
6909077c228a:~$ ./toolchain/bin/armv7l-timesys-linux-gnueabi-gcc
bash: ./toolchain/bin/armv7l-timesys-linux-gnueabi-gcc: No such file or directory
Edit:
Update showing suggestion, don't see any odd character inserted.
6909077c228a% ls -l toolchain/bin/armv7l-timesys-linux-gnueabi-gcc | od -xcb
0000000 722d 7877 2d72 7278 782d 3220 7220 6f6f
- r w x r - x r - x 2 r o o
055 162 167 170 162 055 170 162 055 170 040 062 040 162 157 157
0000020 2074 6f72 746f 3220 3832 3437 3536 5320
t r o o t 2 2 8 7 4 6 5 S
164 040 162 157 157 164 040 062 062 070 067 064 066 065 040 123
0000040 7065 3120 2031 3331 313a 2039 6f74 6c6f
e p 1 1 1 3 : 1 9 t o o l
145 160 040 061 061 040 061 063 072 061 071 040 164 157 157 154
0000060 6863 6961 2f6e 6962 2f6e 7261 766d 6c37
c h a i n / b i n / a r m v 7 l
143 150 141 151 156 057 142 151 156 057 141 162 155 166 067 154
0000100 742d 6d69 7365 7379 6c2d 6e69 7875 672d
- t i m e s y s - l i n u x - g
055 164 151 155 145 163 171 163 055 154 151 156 165 170 055 147
0000120 756e 6165 6962 672d 6363 000a
n u e a b i - g c c \n
156 165 145 141 142 151 055 147 143 143 012
Depending on how you typed in your initial ls -l line, there may be funny characters in the file name. If you use auto completion, it may have put those funny characters in for you so, if you subsequently attempt to type in the file name without auto completion, that could result in a file not found situation.
The first thing you should do is to check the filename completely, with something like:
ls -l toolchain/bin/armv7l-timesys-linux-gnueabi-gcc | od -xcb
and check the output to ensure there's no funny characters in the name.
If the file does exist in that for (no funny characters), one other possibility is that you're trying to run a 32-bit ELF program on a system that's not correctly set up to run them (i.e., a 64-bit system without the libraries and support infrastructure for 32-bit).
That results in an unhelpful error message since it really should be complaining about not being able to find the loader for your 32-bit executable, rather than the executable itself.
If this is the case, you will need to identify those missing items and install them.
I'm trying to use this lovely script on logout from an Ubuntu box that I'm ssh'd into. If I invoke it manually, the colours are correct. However, whenever it's run from .bash_logout, every line is white and prefixed with the colour code:
\x1B[38;5;160;01m .d8888b. 8888888888 8888888888 Y88b d88P .d88888b. 888 888
\x1B[38;5;196;01m d88P Y88b 888 888 Y88b d88P d88P" "Y88b 888 888
\x1B[38;5;202;01m "Y888b. 8888888 8888888 Y888P 888 888 888 888
\x1B[38;5;208;01m "Y88b. 888 888 888 888 888 888 888
\x1B[38;5;214;01m "888 888 888 888 888 888 888 888
\x1B[38;5;220;01m Y88b d88P 888 888 888 Y88b. .d88P Y88b. .d88P
\x1B[38;5;226;01m "Y8888P" 8888888888 8888888888 888 "Y88888P" "Y88888P"
TERM is set to xterm-256color in both the VM and my host (OS X 10.11). I tried using echo -e, but all it did was prepend the colour string with an -e. Adding the contents of the script directly to .bash_logout works, though that seems messy. Thoughts?
In POSIX shell, you cannot use \xXX escapes in the printf command; you can only use octal notation. Using . or source executes your script by the current bash process instead of using a POSIX shell. bash seeyou would have worked as well.
A POSIX-compliant version of your script would change the first line to:
ESC_SEQ="\033[38;5;"
Of course, there is no particular reason to make code executed only from .bash_logout POSIX-compliant, since the file itself is specific to bash.
I tried the following command
for i in `ls`; do ls $i; done
and got the following output:
ls: a.out: No such file or directory
ls: c: No such file or directory
ls: contest: No such file or directory
ls: cpp: No such file or directory
ls: java: No such file or directory
ls: : No such file or directory
It is confusing since the list of files was also obtained using ls. When I tried to do an od on echo, i see the following:
0000000 033 133 060 155 033 133 060 061 073 063 062 155 141 056 157 165
033 [ 0 m 033 [ 0 1 ; 3 2 m a . o u
0000020 164 033 133 060 155 012
t 033 [ 0 m \n
0000026
0000000 033 133 060 061 073 063 064 155 143 033 133 060 155 012
033 [ 0 1 ; 3 4 m c 033 [ 0 m \n
0000016
0000000 033 133 060 061 073 063 064 155 143 157 156 164 145 163 164 033
033 [ 0 1 ; 3 4 m c o n t e s t 033
0000020 133 060 155 012
[ 0 m \n
0000024
0000000 033 133 060 061 073 063 064 155 143 160 160 033 133 060 155 012
033 [ 0 1 ; 3 4 m c p p 033 [ 0 m \n
0000020
0000000 033 133 060 155 146 151 154 145 056 164 170 164 033 133 060 155
033 [ 0 m f i l e . t x t 033 [ 0 m
0000020 012
\n
0000021
0000000 033 133 060 061 073 063 064 155 152 141 166 141 033 133 060 155
033 [ 0 1 ; 3 4 m j a v a 033 [ 0 m
0000020 012
\n
0000021
0000000 033 133 155 012
033 [ m \n
0000004
What does these "033 [ 0 m" characters stand for? How do I avoid them? Are they the cause of this problem?
Please help.
Thanks,
Karthick S.
You don't need `ls` or $(ls). You can use * instead. This way you avoid fancy colored outputs while leaving your code both portable, readable and compact.
This is #1 in Bash Pitfalls
NEVER use ls as input for another command...
The "033 [ 0 m" characters are escape codes for colouring terminal output. Try using this instead:
for file in $(unset LS_COLORS \ls);
do
ls "$file";
done
Or
for file in $(ls -1 -Q --quoting-style=shell --color=never);
do
ls "$file";
done