我想使用一行Perl命令來更改Bash變量中的數據。我認爲問題在於Perl一行代碼沒有收到數據管道。通過Perl更改Bash變量
我知道bash的變化而變化 即findString=${findString//\//\\/}
我很好奇讓Perl來工作過。我不知道Perl,所以保持簡單。
要清楚,這兩行不起作用: 我希望將文本中的選項卡更改爲\ t。我希望任何Unix行結束被改爲\ n。
findString=$(cat "${findString}" | perl -0777pe 's/\t/\\t/g')
findString=$(cat "${findString}" | perl -0777pe 's/\n/\\n/g')
這裏是我的bash代碼:
#!/bin/bash
#The idea here is to change tab to \n
# and line End to \n
# debug info
export PS4='+(${BASH_SOURCE}:${LINENO}):'
# trace all the lines
#set -o xtrace
echo "---------------------- start ----------------------------------------"
# string to change.
# chops off the last \n
read -d '' findString <<"EOFEOFEOF"
# Usage: /Users/mac/Sites/bithoist/commandLine/BitHoist/BitHoist-PPC-MacOS-X [options] input... <input> output
# Options and inputs may be intermixed
-stdin # Use standard input as input file
-offset nn # Offset next input file data by nn
EOFEOFEOF
findString=$(cat "${findString}" | perl -0777pe 's/\t/\\t/g')
findString=$(cat "${findString}" | perl -0777pe 's/\n/\\n/g')
echo "------------> findString of length ${#findString} is:"
echo -E "${findString}"
echo
你想在這些命令中使用'echo',而不是'cat'。 – chepner 2012-08-02 17:55:09
謝謝你的觀察,chepner。我需要回聲。 – historystamp 2012-08-03 18:11:57