Shell 腳本問題:\r 解釋為字符

Shell 腳本問題:\r 解釋為字符

我正在嘗試在新的 NAS 伺服器上建立自己的雲,但使用的腳本不起作用。

腳本:

#!/bin/bash

chown -R http:http /volume1/web/owncloud/
chown -R http:http /volume1/web/owncloud/apps/
chown -R http:http /volume1/web/owncloud/config/
chown -R http:http /volume1/web/owncloud/themes/
chown -R http:http /volume1/owncloud/

chown http:http /volume1/web/owncloud/.htaccess

find /volume1/web/owncloud/ -type f -print0 | xargs -0 chmod 777

find /volume1/web/owncloud/ -type d -print0 | xargs -0 chmod 777
find /volume1/owncloud/ -type d -print0 | xargs -0 chmod 777

chmod 777 /volume1/web/owncloud/.htaccess

答案如下:

Yamakhalah@Jarvis:/$ sudo -su root
Password:
sh-4.3# cd /
sh-4.3# sh /volume1/web/scripts/perm_upd.sh
/volume1/web/scripts/perm_upd.sh: line 2: $'\r': command not found
chown: cannot access ‘/volume1/web/owncloud/\r’: No such file or directory
chown: cannot access ‘/volume1/web/owncloud/apps/\r’: No such file or directory
chown: cannot access ‘/volume1/web/owncloud/config/\r’: No such file or directory
chown: cannot access ‘/volume1/web/owncloud/themes/\r’: No such file or directory
chown: cannot access ‘/volume1/owncloud/\r’: No such file or directory
/volume1/web/scripts/perm_upd.sh: line 8: $'\r': command not found
chown: cannot access ‘/volume1/web/owncloud/.htaccess\r’: No such file or directory
/volume1/web/scripts/perm_upd.sh: line 10: $'\r': command not found
chmod: invalid mode: ‘777\r’
Try 'chmod --help' for more information.
chmod: invalid mode: ‘777\r’
Try 'chmod --help' for more information.
chmod: invalid mode: ‘777\r’
Try 'chmod --help' for more information.
chmod: invalid mode: ‘777\r’
Try 'chmod --help' for more information.
chmod: invalid mode: ‘777\r’
Try 'chmod --help' for more information.
chmod: invalid mode: ‘777\r’
Try 'chmod --help' for more information.
/volume1/web/scripts/perm_upd.sh: line 12: $'\r': command not found
chmod: invalid mode: ‘777\r’
Try 'chmod --help' for more information.
chmod: invalid mode: ‘777\r’
Try 'chmod --help' for more information.
/volume1/web/scripts/perm_upd.sh: line 15: $'\r': command not found
sh-4.3#

有人可以解釋為什麼\r到處都會出現這種情況嗎?

答案1

您可能正在使用 Windows 樣式檔案並將其複製到您的 NAS。你必須將其轉換為 Unix 風格。維基百科連結到更詳細的解釋。某些編輯器能夠將文件儲存為其他格式。如果您的編輯器無法執行此操作,您可以使用dos2unix大多數 Linux 發行版可用的實用程式。

如果上述所有選項都沒有幫助,這個堆疊溢位也許可以幫助你。

相關內容