的bash腳本莫名其妙地失去了所有尾隨換行,但我不知道在哪裏,或者如何讓他們護尾隨功能結果換行符在bash
#!/bin/bash
function rep {
FIRST=${1// /" "}
SECOND=${FIRST//&t;/" "}
echo "${SECOND//&nl;/"
"}"
}
if [ "$7" == "" ]
then
cd "$(rep $6)"
th sample.lua -checkpoint cv/"$(rep $1})"/"$(rep $2)" -length $3 -temperature $4 -sample 1 > samples/"$(rep $1)"/Sample-$5.txt
else
cd "$(rep $7)"
th sample.lua -checkpoint cv/"$(rep $1)"/"$(rep $2)" -length $3 -temperature $4 -sample 1 -start_text "$(rep $5)" > samples/"$(rep $1)"/Sample-$6.txt
fi
在這個腳本的功能rep
代替我做了標記在替換爲空格,製表符和換行符的輸入字符串中。我很確定任何空格和換行符都會被刪除。
我已經看過這樣的解決方案,https://stackoverflow.com/a/15184414/5332233,但我還沒有能夠讓它在代碼中工作。
編輯 - 這裏的時候,我打算給虛擬人物添加到變量
function rep {
FIRST="${1// /" "}"
SECOND="${FIRST//&t;/" "}"
THIRD="${SECOND//&nl;/"
"}"
a=$(printf $THIRD; printf x); echo ${a%x}
}
這裏的另一種嘗試,這仍然擺脫所有尾隨換行符的
#!/bin/bash
function rep {
IN="$1x"
FIRST="${IN// /" "}"
SECOND="${FIRST//&t;/" "}"
THIRD="${SECOND//&nl;/"
"}"
echo "${THIRD%x}"
}
echo "$(rep $5)"
if [ "$7" == "" ]
then
cd "$(rep $6)"
th sample.lua -checkpoint cv/"$(rep $1})"/"$(rep $2)" -length $3 -temperature $4 -sample 1 > samples/"$(rep $1)"/Sample-$5.txt
else
cd "$(rep $7)"
th sample.lua -checkpoint cv/"$(rep $1)"/"$(rep $2)" -length $3 -temperature $4 -sample 1 -start_text "$(rep $5)" > samples/"$(rep $1)"/Sample-$6.txt
fi
是;命令替換剝離*尾隨*新行。鏈接的答案提供了幾種解決方法,以避免這種情況不可取;你嘗試過什麼? – chepner
我試着追加一個字符,並在命令中使用它時將其刪除,但我無法獲得該工作,而另外兩個字符我找不到正確的方法來實現它 – Dankrushen
它會如果你顯示了你所嘗試的內容,那就幫忙 – chepner