Linux命令行特殊字符M-BM字符处理?


# 场景描述

遇到同事执行命令始终抱错,以为是role问题,因为Google的这个warning太弱智了

如下

`gcloud iam service-accounts add-iam-policy-binding --role roles/iam.workloadIdentityUser --member "serviceAccount:English-dev.svc.id.goog[namespace/English-dev-sql-sa]" cloudsql-user@chinaunux-dev.iam.gserviceaccount.com --project chinaunux-dev`

报错如下:

ERROR: Policy modification failed. For a binding with condition, run "gcloud alpha iam policies lint-condition" to identify issues in condition.

ERROR: (gcloud.iam.service-accounts.add-iam-policy-binding) INVALID_ARGUMENT: The role name must be in the form "roles/{role}", "organizations/{organization_id}/roles/{role}", or "projects/{project_id}/roles/{role}".

M-BM字符是字节序列0xc2 0xa0的ASCII表示,它是unicode字符A0的UTF8编码,是一个非中断空格字符。使用键序列strl+shift+space,可以在LibreOffice和Microsoft Word文档中插入此字符

根本原因是特殊字符的原因

所以分析看下:

排除特殊字符的时候cat是个好工具,先看下用法

```bash

cat --help

-A, --show-all equivalent to -vET

-v, --show-nonprinting use ^ and M- notation, except for LFD and TAB

```

$ file a.sh

a.sh: Unicode text, UTF-8 text

$ file -bi a.sh

text/plain; charset=utf-8

$ cp a.sh b.sh

$ vi b.sh 手动编辑去掉特殊字符

$ cat -v b.sh

gcloud iam service-accounts add-iam-policy-binding --role roles/iam.workloadIdentityUser --member "serviceAccount:English-dev.svc.id.goog[namespace/English-dev-sql-sa]" cloudsql-user@chinaunux-dev.iam.gserviceaccount.com --project chinaunux-dev

$ file -bi b.sh

text/plain; charset=us-ascii

# 给一些自动替换的办法

$ cp a.sh c.sh

$ sed -i 's/ / /g' c.sh

$ file -bi c.sh

text/plain; charset=us-ascii

# 搜索下

https://askubuntu.com/questions/357248/how-to-remove-special-m-bm-character-with-sed

#!/bin/bash
#############################################################################
# SCRIPT:   M-BM-Remover.sh
# DESCRIPTION:
#           This script will be able to detect hidden caracter "M-BM-",
#               And/Or remove this !
# REVISIONS:
#           2014/06/11  YG
#____________________________________________________________________________
#
# PARAMETERS:
#  > $1  :TARGET,      (e.g. '"*.sh"' )
#  > $2  :ACTION,      (e.g. 'remove' )
#  > $2  :BACKUP,      (e.g. '' )
#
#############################################################################

TARGET=$1
ACTION=$2
BACKUP=$3

if [ "$TARGET" = "" ]
then
    echo 'Need to choose target file'
    echo 'M-BM-Remover [TARGET] [show/remove] [backup]'
    echo 'Example : M-BM-Remover "*.sh" remove backup'
    exit
fi

echo "ACTION = $ACTION";
echo "TARGET = $TARGET";
echo

if [ "$ACTION" = "show" ]
then
    for file in $TARGET
    do
        if [ "$file" != "M-BM-Remover.sh" ]
        then
            echo "Traitement de $file ..."
            cat -v $file | grep M-BM-
            NB=`cat -v $file | grep M-BM- | wc -l`
            echo "Occurence(s) : $NB"
        fi
    done 
fi

if [ "$ACTION" = "remove" ] || [ "$ACTION" = "" ]
then
    for file in $TARGET
    do
        if [ "$file" != "M-BM-Remover.sh" ]
        then
            echo "Traitement de $file ..."
            NB=`cat -v $file | grep M-BM- | wc -l`
            if [ "$BACKUP" = "backup" ]
            then
                cat $file > $file.bak
            fi
            cat -v $file.bak | sed s/M-BM-//g > $file
            echo "Occurence(s) removed : $NB"
        fi
        echo
    done 
fi
展开阅读全文

页面更新:2024-04-15

标签:字符   弱智   空格   序列   字节   场景   同事   命令   编辑   原因

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top