gpt4 book ai didi

linux - 获取 Linux 中的当前命令

转载 作者:太空宇宙 更新时间:2023-11-04 03:34:03 24 4
gpt4 key购买 nike

我正在创建一个 shell 脚本,它将始终从 shell 读取用户的当前命令。

我目前正在使用 read 命令,但我对此不满意,因为它用于提示问题。示例:

root@hostname: ./script.sh
Question here?
answer - `read` command

我希望当用户直接在命令行上输入命令时调用我的脚本(登录后脚本已经通过/etc/profile.d/myapp.sh 运行)。

root@hostname: read the command here
result will happen

我的示例脚本myapp.sh:

#!/bin/bash
if [ "input" = "value"]
then
do some actions
fi

最佳答案

如果您明确针对 Bash,我建议为这些命令设置 Bash 别名。

在用户 session 开始时,您的程序可以运行以下命令来创建 bash 别名:

alias command1="/your/path/script.sh command1"
alias command2="/your/path/script.sh command2"

然后,例如,当用户输入 command1 时,它将展开并运行 /your/path/script.sh command1

关于linux - 获取 Linux 中的当前命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32263509/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com