linux - 如何通过三个或多个zsh程序传递变量?

标签 linux ubuntu zsh

所以我制作了几个脚本来帮助自己,但显然在转移到不同的操作系统(从 mint 到 ubuntu)并安装 zsh 之后,它们停止工作了。 这是我的案例之一,称为“cweb”脚本,它应该让我进入特定目录(通过终端)。但显然 $location 没有在整个脚本中被编辑,因此我没有移动目录,有人知道吗? 我承认从那以后我对代码做了一些修改,但它们并没有像我预期的那样工作。

如果有人可以看一下并在这里帮助我..

我打开终端并输入“cweb”

cweb
What would you like to run?
Press f to frontend, b to backend. [f/b]
f
Reading frontend directory ('frontend_dir') text file..
Work Directory Location Exported.

Moving onto the chosen directory..
There you go!

所以当我在终端 vim ~/.zshrc 中输入时 我写了

SCRIPTS_DIR=/home/skillz/Desktop/Desktop_Items/Work/Scripts
export PATH="$PATH:$SCRIPTS_DIR"
export SCRIPTS_DIR=$SCRIPTS_DIR/Supporting_Scripts

在脚本路径中我有'cweb'脚本

网络:

#!/bin/zsh
$SCRIPTS_DIR/ask_server
echo "Moving onto the chosen directory.."
cd $location
echo "There you go!"
exec zsh

询问服务器:

#!/bin/zsh
echo "What would you like to run?"
while true; do
  echo "Press f to frontend, b to backend. [f/b]"
  read -k 1 fb
  echo
  case $fb in
    [Ff]* ) zsh $SCRIPTS_DIR/read_frontend_dir && break;;
    [Bb]* ) zsh $SCRIPTS_DIR/read_backend_dir && break;;
    * ) echo "Unrecognized Option."
  esac
done
echo $location

read_frontend_dir:

#!/bin/zsh
echo "Reading frontend directory ('frontend_dir') text file.."
file=$SCRIPTS_DIR/Dirs/frontend_dir.txt
while IFS= read -r line
do
  location="$line"
done <"$file"
echo "Work Directory Location Exported."

read_backend_dir:

#!/bin/zsh
echo "Reading backend directory ('backend_dir') text file.."
file=$SCRIPTS_DIR/Dirs/backend_dir.txt
while IFS= read -r line
do
  location="$line"
done <"$file"
echo "Work Directory Location Exported."

前端目录.txt

/home/skillz/cweb-frontend/

后端目录.txt

/home/skillz/skillz-coding-website/

长话短说 我运行一个名为 CWEB 的脚本,它会提示我一个问题。 我可以键入 f 或 b,根据我的选择,它将读取不同的目录并将我传递到该目录。 该脚本运行良好,它让所有正确的脚本一个接一个地运行。但最终 cd-ing 到返回的位置不起作用。 我在收到它后打印了 $location 并且显然它是空的.. 有什么线索吗?

有人知道为什么它不将我移至“选择的目录”吗? 它确实存在并且 cd 可以正常工作。

最佳答案

那是行不通的,因为 read 是内置的 Bash shell。 zsh 也有一个 read 函数,但工作方式有点不同。

来自 Zsh 文档:

-n
Together with -c, the number of the word the cursor is on is read. With -l, the index of the character the cursor is on is read. Note that the command name is word number 1, not word 0, and that when the cursor is at the end of the line, its character index is the length of the line plus one.

例子:

# Zsh:
% read -n 1 tmp
ls
% echo $tmp

% read tmp
ls
% echo $tmp
ls
% read -n 1 tmp
ls sl
% echo $tmp
sl

如果您仍想迁移到 Zsh,您需要的是“-k”选项:

-k [ num ]
Read only one (or num) characters. All are assigned to the first name, without word splitting. This flag is ignored when -q is present. Input is read from the terminal unless one of -u or -p is present. This option may also be used within zle widgets.

Note that despite the mnemonic ‘key’ this option does read full characters, which may consist of multiple bytes if the option MULTIBYTE is set.

Bash read manual
Zsh read manual (滚动阅读)。

关于linux - 如何通过三个或多个zsh程序传递变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57756484/

相关文章:

ubuntu - 不支持 Ubuntu 中的 Docker 将文件系统切换到覆盖?

python - 如何修复 "update-alternatives: warning: forcing reinstallation of alternative/usr/bin/python3.8 because link group python3 is broken"?

PHP 扩展在 Ubuntu 中激活

arrays - 在每个数组条目的开头添加字符串

linux - 如何使用 GIMP 编写自定义自动裁剪脚本?

c - 网络设备中断后谁要求调度?

emacs - 如何在 emacs 中获取我的 .zshrc?

zsh - 搜索后总是返回到 ZSH 历史记录的开头?

linux - ssh 使用私钥无需密码

linux - 如何更改 Linux 中的组所有权?