免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5227 | 回复: 9
打印 上一主题 下一主题

[文件目录] 关于mkdir 在shell脚本里没办法识别~目录 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-06-18 23:13 |只看该作者 |倒序浏览
为了学习shell,我就想先练习一下,自己写一个挂载U盘、卸载U盘、移动U盘中的文件的脚本,现在在新建一个目录的时候出现了问题,mkdir "$location" 出错,如果我输入/root/usb就可以正常运行,如果输入~/usb就说无法找到目录什么的,请问大神这是怎么回事。另外,我对shell中的双引号感到很奇怪,就比如这个问题,mkdir $location应该也行吧?那为什么看到的书上都加了双引号?谢谢大神。。求助
  1. #!/bin/bash

  2. #This program is used for mount the usb_disk, umount it, and cp/rm/mv the files

  3. #Edition: v 1.0

  4. #Time:2013.06.18



  5. echo "Please make sure your U disk is inserted!"
  6. read -p "do you want to mount or umount? (M/U)" object

  7. if [ $object == M ] || [ $object == m ]; then
  8.        
  9.         read -p "Please input the location you want to mount the U disk:" location
  10.        
  11.         echo $location
  12.        
  13.         if [ -d $location ];        then
  14.                
  15.                 echo "Content has existed"
  16.        
  17.         else
  18.                
  19.                 mkdir "$location"
  20.        
  21.         fi
  22.        
  23.         mount /dev/sdb1 "$location"
  24.        
  25.         if [ $? != 0 ];        then
  26.                
  27.         echo "ERROR! please ensure your UDISK is inserted or has not been mounted!!!"
  28.         fi

  29. else
  30.        
  31.         umount /dev/sdb1
  32.        
  33.         echo $?
  34. fi
复制代码

论坛徽章:
4
白羊座
日期:2013-09-17 21:59:30技术图书徽章
日期:2013-10-12 22:16:03白羊座
日期:2013-10-14 11:01:40双子座
日期:2013-12-17 18:26:39
2 [报告]
发表于 2013-06-19 00:37 |只看该作者
回复 1# 陆地蛮牛
~扩展在变量扩展前面进行,所以$location扩展为~/usb后,不会再将~扩展为/root。


   

论坛徽章:
6
摩羯座
日期:2013-08-24 10:43:10狮子座
日期:2013-08-25 10:27:06天秤座
日期:2013-09-11 20:28:44午马
日期:2014-09-28 16:06:0015-16赛季CBA联赛之八一
日期:2016-12-19 13:55:0515-16赛季CBA联赛之天津
日期:2016-12-20 14:01:23
3 [报告]
发表于 2013-06-19 08:11 |只看该作者
双引号的问题本版置顶贴里有篇shell十三问,仔细看看,定有收获。

论坛徽章:
0
4 [报告]
发表于 2013-06-19 10:32 |只看该作者
但当我插第二个U盘这shell就不行了

论坛徽章:
0
5
发表于 2013-06-19 13:12
我还是不太明白,请您再讲解一些,如果不扩展为/root,那它被识别为什么呢?谢谢回复 2# 井蛙夏虫


   

论坛徽章:
0
6 [报告]
发表于 2013-06-19 13:13 |只看该作者
是的呢。。。我就是练习一下shell,真实用的话估计不行回复 4# all17lai


   

论坛徽章:
0
7 [报告]
发表于 2013-06-19 13:14 |只看该作者
好的,我去翻翻看,谢谢您回复 3# cao627


   

论坛徽章:
0
8 [报告]
发表于 2013-06-19 15:02 |只看该作者
再次求助,比如我把U盘挂载到了两个地方,最后我umount /dev/sdb1的时候只卸载了一个,这怎么办?回复 2# 井蛙夏虫


   

论坛徽章:
4
白羊座
日期:2013-09-17 21:59:30技术图书徽章
日期:2013-10-12 22:16:03白羊座
日期:2013-10-14 11:01:40双子座
日期:2013-12-17 18:26:39
9 [报告]
发表于 2013-06-19 17:53 |只看该作者
回复 8# 陆地蛮牛
man umount
  1. The  umount  command detaches the file system(s) mentioned from the file hierarchy.  A file system is specified by giving the directory where it has been mounted. Giving the special device on which the file system lives may also work, but is obsolete, mainly because it will fail in case this device was mounted on more than one directory.
复制代码
所以,你最好使用umount dir而不是umount dev的形式。


   

论坛徽章:
0
10 [报告]
发表于 2013-06-20 20:13 |只看该作者
嗯,我明白了,谢谢
回复 9# 井蛙夏虫


   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP