[問題] getopt() 參數為"\n"

看板C_and_CPP作者 (smigo)時間12年前 (2012/03/13 00:43), 編輯推噓2(201)
留言3則, 2人參與, 最新討論串1/1
使用語言: C 最近在使用getopt的時候發生一個問題 程式碼大概如下 case 'd': strcpy(pat,optarg); break; 這時候如果再command line輸入 ./xxx -d "AA\nBB" 在GDB裡看到的pat會是"AA\\nBB" 預期的結果是"AA\nBB" 好像會幫我自動做'\'的判斷 不知道是shell的問題還是getopt的問題? 已經試過改成'AA\nBB' 結果也一樣 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.123.214.45

03/13 01:11, , 1F
Shell 不會把 \n 轉成 char 0x0a, 用 ./xxx -d "AA
03/13 01:11, 1F

03/13 01:11, , 2F
BB" 或者要自己手動把 "\\n" 轉成 '\n'
03/13 01:11, 2F

03/13 09:33, , 3F
bash: ./xxx -d $"AA\nBB", or sh: ./xxx -d `echo 'AA\nBB'`
03/13 09:33, 3F
文章代碼(AID): #1FNYUkvj (C_and_CPP)