加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – 如何将密码作为参数传递给shell脚本

发布时间:2020-12-14 00:35:22 所属栏目:Linux 来源:网络整理
导读:我正在通过ansible-playbook自动安装鼠尾草.在那里我需要运行两个 shell脚本. 以下是第一个shell脚本的外观: #!/bin/bash# Creating Sage notebookdir="/root/.sage/sage_notebook.sagenb"screen -S "Sage_Server" sage -c 'notebook(interface="",director
我正在通过ansible-playbook自动安装鼠尾草.在那里我需要运行两个 shell脚本.
以下是第一个shell脚本的外观:

#!/bin/bash
# Creating Sage notebook

dir="/root/.sage/sage_notebook.sagenb"
screen -S "Sage_Server" sage -c 'notebook(interface="",directory=$dir,port=80,accounts=true)'

这是第二个shell脚本的代码:

#!/bin/bash
# Creating Sage inotebook

address=$(hostname --ip-address)
sage -c "inotebook(interface=" "'$address'" ",accounts=true)"

这就是剧本的样子:

---
- hosts: localhost
  remote_user: root

  tasks:
   - name : update system
     apt : update_cache=yes    

   - name : install m4
     apt : name=m4 state=present

   - name : install build-essential
     apt : name=build-essential state=present 

   - name : install gcc
     apt : name=gcc state=present

   - name : install gfortran
     apt : name=gfortran state=present

   - name : install libssl-dev
     apt : name=libssl-dev state=present

   - name : install python-software-properties
     apt : name=python-software-properties state=present

   - name : add sage ppa repo
     apt_repository: repo='ppa:aims/sagemath'

   - name : update system
     apt : update_cache=yes

   - name : install dvipng
     apt : name=dvipng state=present

   - name : install sage binary
     apt : name=sagemath-upstream-binary state=present

   - name : invoke create_sagenb script
     command: /usr/bin/screen -d -m sudo /root/databases-and-datamining-iiith/python-scripts/create_sagenb -i -y

   - name : invoke start_sage script
     command: /usr/bin/screen -d -m sudo /root/databases-and-datamining-iiith/python-scripts/start_sage -i -y

现在,当我运行第一个脚本时,它会要求输入一个新的sage密码.但是我无法从剧本中传递密码.
如果我这样做的话

ps -ef | grep sh

我可以看到脚本正在运行但是sage服务没有运行.
它需要密码才能启动服务.

有谁能告诉我如何通过命令提供密码作为shell脚本的参数.

解决方法

根据经验,您不应该在ansible playbooks中运行需要用户输入的脚本.

你可以试着用类似的东西

echo "password" | script.sh

要么

在/ etc中创建一个包含密码的sage-password文件,并且:

script.sh < /etc/sage-password

但这只有在从stdin读取时才会起作用 – 大多数应用程序直接从终端设备驱动程序读取密码(即/ dev / ttyS#),在这种情况下,这个技巧将不起作用.

如果是这种情况,请查看sage文档,他们应该有一个更强大的非交互式启动方式.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读