linux - 将文件中读取的路径存储在变量中(脚本 shell)

标签 linux bash shell

我有一个文件,里面有几个路径,形状是:

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/

相关文章:

Linux 对话框用户输入直接到变量

bash - 将 bash 变量传递给内联 perl 脚本

regex - 来自文件的 Bash IP

shell - Ubuntu crontab 编辑

linux - 如何删除两种模式之间的 block 文本?

bash getopts 多个参数或默认值

linux - 通过伪 tty 终端的 SSH 和 sudo

linux - TCP数据丢失事件

c - Linux页面错误内存中的互斥锁

git - git 预提交中的 autopep8——如何自动重新提交?