如何将用户输入读入 Bash 中的变量?

编程入门 行业动态 更新时间:2024-10-21 13:29:27
本文介绍了如何将用户输入读入 Bash 中的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试创建一个脚本来简化在 iOS 设备上创建新用户的过程.以下是分解步骤.

I'm trying to create a script that simplifies the process of creating a new user on an iOS device. Here are the steps broken down.

fullname="USER INPUT" user="USER INPUT" group=$user uid=1000 gid=1000 home=/var/$user echo "$group:*:$gid:$user" >> /private/etc/group echo "$user::$uid:$gid::0:0:$fullname:$home:/bin/sh" >> /private/etc/master.passwd passwd $user mkdir $home chown $user:$group $home

如您所见,某些字段需要输入.如何在脚本中请求输入变量?

As you can see some fields require input. How can I request input for a variable in script?

推荐答案

使用read -p:

# fullname="USER INPUT" read -p "Enter fullname: " fullname # user="USER INPUT" read -p "Enter user: " user

如果您想确认:

read -p "Continue? (Y/N): " confirm && [[ $confirm == [yY] || $confirm == [yY][eE][sS] ]] || exit 1

您还应该引用您的变量以防止路径名扩展和用空格进行分词:

You should also quote your variables to prevent pathname expansion and word splitting with spaces:

# passwd "$user" # mkdir "$home" # chown "$user:$group" "$home"

更多推荐

如何将用户输入读入 Bash 中的变量?

本文发布于:2023-11-22 06:46:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616374.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   如何将   用户   Bash

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!