如何修复 ~/.profile 和 ~/.bashrc 语法错误

Hed*_*ger 4 upgrade .profile bashrc syntax 18.04

Ubuntu 18.04.4(几周前从 18.04.3 自动升级)
/bin/bash

从 Gnome shell 登录后,当屏幕为纯紫色且桌面出现之前,会出现此消息:

Error found when loading /home/hfinger/.profile:  

/home/hfinger/.profile: line 1: #: command not found  
/home/hfinger/.bashrc: line 1: syntax error near unexpected token '('
/home/hfinger/.bashrc: line 1: '# ~/.bashrc: executed by bash(1) for non-login shells.'  

As a result the session will not be configured correctly.  
You should fix the problem as soon as feasible.  
Run Code Online (Sandbox Code Playgroud)

问题是,有什么问题?我已经搜索过 AskUbuntu,但似乎没有人遇到过这个确切的问题。我不认为这是升级,因为在出​​现此问题之前它已经运行了大约一个月。

我从来没有接触过这两个文件,因为我很高兴让系统创建和配置它们。此外,我尽量让 Ubuntu 保持原样,以避免在升级到新版本后必须恢复设置。值得一提的是,以下是每个文件的第一行:

.profile, line 1: # ~/.profile: executed by the command interpreter for login shells.  
.bashrc, line 1: # ~/.bashrc: executed by bash(1) for non-login shells.  
Run Code Online (Sandbox Code Playgroud)

我需要如何修复这些文件?我应该提供哪些附加信息,以便比我知识渊博的人能够解决这个愚蠢的问题?

ste*_*ver 5

如果您的文件在开始时具有非打印字节序列(例如字节顺序标记),则可能会发生这种情况,这可能是由于在文字处理器程序或 Windows 文本编辑器中对其进行了编辑。

例如,给定

$ file profile bashrc
profile: UTF-8 Unicode (with BOM) text
bashrc:  UTF-8 Unicode (with BOM) text
Run Code Online (Sandbox Code Playgroud)

(其中profilebashrc是 my~/.profile和在开头插入~/.bashrc字节序列的本地副本0xFE 0xFF)然后

$ bash -c 'source profile; source bashrc'
profile: line 1: #: command not found
bashrc: line 1: syntax error near unexpected token `('
bashrc: line 1: `# ~/.bashrc: executed by bash(1) for non-login shells.'
Run Code Online (Sandbox Code Playgroud)

最简单的修复方法是使用dos2unix,默认情况下会删除 BOM:

$ dos2unix profile bashrc
dos2unix: converting file profile to Unix format...
dos2unix: converting file bashrc to Unix format...

$ file profile bashrc
profile: ASCII text
bashrc:  UTF-8 Unicode text
Run Code Online (Sandbox Code Playgroud)

或者,您可以简单地用/etc/skel您发现的目录中的新副本替换文件(尽管显然您会以这种方式丢失任何自定义)。


检查非打印字节的其他方法是

cat -A ~/.profile ~/.bashrc
Run Code Online (Sandbox Code Playgroud)

其中 BOM 将显示为控制序列,例如M-oM-;M-?#或使用xxdod直接检查字节序列

head -1 ~/.profile | od -tx1

xxd -l16 ~/.profile
Run Code Online (Sandbox Code Playgroud)

diff命令是有帮助的,只要确认有有差别,但不识别的区别是什么:

$ diff profile ~/.profile
1c1
< # ~/.profile: executed by the command interpreter for login shells.
---
> # ~/.profile: executed by the command interpreter for login shells.
Run Code Online (Sandbox Code Playgroud)


hey*_*ema 1

检查用户 shell 的简单方法...

如果您尚未Users and Groups安装应用程序,请按以下方式安装...

sudo apt-get update

sudo apt-get install gnome-system-tools

Super键并输入“用户”,选择Users and Groups应用程序,单击帐户名称,然后单击“高级设置”、“高级”选项卡,并验证/bin/bash.


Hed*_*ger 1

Ubuntu 18.04.4(几周前从 18.04.3 自动升级)
/bin/bash

我不知道问题是什么,但这是我解决它的方法:

$ cd ~ 
$ which bash
/bin/bash  
$ mv .profile .profile.bak  
$ mv .bashrc .bashrc.bak  
$ cd /etc/skel/  
$ cp .profile ~/.profile  
$ cp .bashrc ~/.bashrc  
Run Code Online (Sandbox Code Playgroud)

(注意:/bin/bash是 的正确位置bash。)

感谢大家的意见和帮助。