我有一个文件,里面有几个路径,形状是:
directory1/directory2/directory3
我想将这些路径之一放入脚本 shell 中的变量中,以将其用作路径。
我试过了:
name="`sed -n 2p file`"
但我不能使用这样的命令: cd "../$name"
。我收到一条错误消息,指出此文件夹不存在。
您知道如何在我的脚本上执行此操作吗?
最佳答案
使用剪切,
name=`cut -f1 -d "/" file` # instead of f1 use f2 for directory2 and so on
像这样,
[root@giam46 ~]# cat sample.txt
Desktop/MEF_R400
[root@giam46 ~]# name=`cut -f1 -d "/" sample.txt`
[root@giam46 ~]# echo $name
Desktop
[root@giam46 ~]# cd /root/${name}
[root@giam46 Desktop]# pwd
/root/Desktop
[root@giam46 Desktop]# cd ..
[root@giam46 ~]# pwd
/root
[root@giam46 ~]#
关于linux - 将文件中读取的路径存储在变量中(脚本 shell),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25847850/