---
title: "Linux的shell环境下执行文件"
author: "berrywu"
date: "2010-04-09T10:49:51Z"
tags: ["Linux", "PHP", "Unix", "Windows", "vim"]
categories: ["编程语言"]
slug: "linux-shell"
draft: false
lastmod: "2010-04-09T10:51:04Z"
id: 638929
url: "https://berrywu.iteye.com/blog/638929"
---

在Linux的shell环境下执行\[user@hostname filename]#./index.php文件时,经常会报这种错误：

-bash: ./index.php: /usr/local/php5/bin/php^M: bad interpreter: No such file or directory

 

查了下原因才知道，原来是不同系统编码格式引起的。在windows系统中编辑的.sh文件可能有不可见字符，所以在Linux系统下执行会报以上异常信息。

 

我在windows下使用EditPlus远程连接到Linux上进行编辑文件

```
#! /usr/local/php/bin/php
<?php
echo 'this is test';
?>
```

 执行方式：./index.php

 结果报错：

-bash: ./index.php: /usr/local/php5/bin/php^M: bad interpreter: No such file or directory

 

解决问题的办法：

1、在windows下直接转换。

     EditPlus下修改文件类型的位置：点击菜单栏上的 文档-&gt; 文件格式-&gt;更改文件类型 选择UNIX，默认是PC,也可以再参数配置里面事先改好，如下图

![](http://dl.iteye.com/upload/attachment/231489/0fb4b03e-a1a8-38e7-b9f1-cd3483288532.jpg)  
 

2、在Linux下直接转换

   a、Vim编辑index.php文件

   b、利用如下命令查看文件格式 :set ff 或 :set fileformat  
   c、可以看到如下信息 fileformat=dos 或 fileformat=unix 若是unix的就不用改了

   d、利用如下命令修改文件格式 :set ff=unix 或 :set fileformat=unix

   e、:wq (存盘退出)  
再执行一下，OK。

 

备注：一般在shell环境下执行文件是命令 + 文件，如/usr/local/php/bin/php ./index.php

若要直接用./index.php 就按我上述的方法配置好

1、确保文件的可执行性，#chmod 775 index.php

2、将执行命令写在文件里面