我在 Ubuntu 20.04 VM 中創建 shell 腳本,所以我在 bash 中測驗以下內容:
echo ${"/mnt/raid1/"//\//}
我需要實作的是將檔案夾的路由作為有效的變數名,例如“mntraid1”。但是,以前的代碼會引發 bash 錯誤 - 替換錯誤。
任何方向表示贊賞。
uj5u.com熱心網友回復:
只有一個變數名可以在這個引數擴展的第一個位置。因此,您必須先將字串分配給變數,然后再對其進行替換。
#!/usr/bin/env bash
# ^^^^- this makes your script a bash script
# for extra paranoia, one can add an explicit version check
# otherwise, a user running 'sh yourscript' can override the shebang above
case $BASH_VERSION in '') echo "This script requires bash" >&2; exit 1;; esac
dev=/mnt/raid1
var=${dev//'/'/}
echo "Variable name corresponding to $dev is $var"
請注意,這是 POSIX sh 標準不保證的擴展。因此,在任何帶有#!/bin/shshebang 的腳本中使用都是不安全的(它只保證該標準所需的功能)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/463658.html
標籤:重击 嘘 ubuntu-20.04
下一篇:printf將逗號列印到檔案
