当前位置:   article > 正文

Window下编写的sh文件在Linux/Docker中无法使用_windos sh linux

windos sh linux

一、sh文件目的

1.1 初始状态

已经安装nvidia-dockerdocker中创建容器测试nvidia-smi正常

1.2 目的

制作Docker镜像,将Python环境【保存在文件python3_envs.txt中】部署到Docker镜像中,包括pytorchcuda等环境

二、过程与异常

2.1 首先获取标准ubuntu20.04 - 正常

sudo nvidia-docker pull ubuntu:20.04
  • 1

2.2 启动ubuntu20.04容器 - 正常

>>>sudo nvidia-docker run -it -d --name=demo --gpus=all -v /home/download:/root/download ubuntu:20.04 /bin/bash
55d0ded464ac82cab65befa54ec749fcff61a638ecf57a96f22aae3b95428b64
>>>sudo nvidia-docker attach demo
root@55d0ded464ac:/#
  • 1
  • 2
  • 3
  • 4

2.3 执行windows下写的preInstall文件 - 报错

>>>root@55d0ded464ac:/#./preInstall.sh
 sh preInstall.sh 
: not foundsh: 2: 
E: Invalid operation update
  • 1
  • 2
  • 3
  • 4

使用vimlinux下编辑preInstall.sh,未能解决问题。

三、检查和处理

3.1 评估异常

在windows下用notepad++打开preInstall.sh,如下图,评估可能是因为行尾和linux格式不一致【因为这个是常见问题】
在这里插入图片描述

3.2 处理异常

用notepad++执行行尾转换【状态栏操作,如小图】,将行尾转换为正常linux格式
在这里插入图片描述
在这里插入图片描述

3.3 调整后运行测试 - 正常

>>>root@55d0ded464ac:/#./preInstall.sh
Hit:1 http://security.ubuntu.com/ubuntu focal-security InRelease
Hit:2 http://archive.ubuntu.com/ubuntu focal InRelease
Get:3 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Hit:4 http://archive.ubuntu.com/ubuntu focal-backports InRelease
Get:5 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [3895 kB]
Get:6 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [1480 kB]                       Fetched 5489 kB in 9s (625 kB/s)                                                                             Reading package lists... Done
......
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

3.4 sh运行完,检查docker容器 - 完成

>>>root@55d0ded464ac:/#python3
Python 3.8.10 (default, Nov 22 2023, 10:22:35) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> print(torch.cuda.is_available())
True
>>> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

四、小结

linux下运行从其他操作系统编写的sh文件时,需要检查文件行尾是否为linux格式


好记性不如烂笔头。

站在岸上学不会游泳。

眼看千遍,不如手过一遍。


may the odds be ever in your favor ~

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/720447
推荐阅读
相关标签
  

闽ICP备14008679号