0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Microsoft Azure Cloud Shell等のscriptコマンド結果に [?2004h とか [?2004l とか表示される

0
Posted at

Webターミナルを利用する際に、scriptコマンドで記録開始&exitコマンドで記録終了し、カレントディレクトリにファイル保存させることもあるかと思います。

その際、windowsに作業結果ファイルをダウンロードして表示すると以下のような制御文字付きの結果になってしまわないようにするための改善策です。

sample
Script started on 2026-04-27 07:24:24+00:00 [TERM="xterm-256color" TTY="/dev/pts/0" COLUMNS="187" LINES="101"]
[?2004h[1;32muser001 [ [0m~[1;32m ]$ [0m
[?2004l
[?2004h[1;32muser001 [ [0m~[1;32m ]$ [0m
[?2004l
[?2004h[1;32muser001 [ [0m~[1;32m ]$ [0m
[?2004l
[?2004h[1;32muser001 [ [0m~[1;32m ]$ [0m[7mecho "abcd"[27m

[A[C[C[C[C[C[C[C[C[C[C[C[C[C[C[C[C[C[C[C[C[Cecho "abcd"

[A
[?2004l
abcd
[?2004h[1;32muser001 [ [0m~[1;32m ]$ [0m

記録開始時

ファイル名はsession_yyyymmdd_HHMMSS.logと出力するようにしています。

start
bind 'set enable-bracketed-paste off' 2>/dev/null
LOGFILE="session_$(date +%Y%m%d_%H%M%S).log"
SHELL=/bin/bash script "$LOGFILE"

記録終了時

元ファイルとは別に、追加ファイルとしてsession_yyyymmdd_HHMMSS_clean.logを出力するようにしています。

end
exit
sed -r "s/\x1B\[[0-9;?]*[a-zA-Z]//g; s/\x1B[=>]//g; s/\r//g" "$LOGFILE" > "${LOGFILE%.log}_clean.log"

対応後

sample_clean
Script started on 2026-04-27 07:24:24+00:00 [TERM="xterm-256color" TTY="/dev/pts/0" COLUMNS="187" LINES="101"]
user001 [ ~ ]$ 
user001 [ ~ ]$ 
user001 [ ~ ]$ 
user001 [ ~ ]$ echo "abcd"
echo "abcd"

abcd
user001 [ ~ ]$ 

解説

原因はenable-bracketed-paste機能とANSI制御文字のwindows側表示崩れということにあります。
そのため、記録開始時にenable-bracketed-paste機能をoffにし、表示崩れはどうしようもないので事後にsedコマンドを利用して除去している、という感じです。

#もっと簡単な改善策があればいいんですが・・・

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?