#!/bin/bash
# This tells you that the script must be run by root.
if [ "$(id -u)" != "0" ];
  then
echo "This script must be run as root!" 1>&2
exit 1
fi

userexists=0
while [ $userexists -eq 0 ]
do
# This asks for the user's input for a username.
  echo -n "Enter a username: "
  read username

  x="$username:x:"
# This if block checks if the username exists.
  grep -i $x /etc/passwd > /dev/null
  if [ $? -eq 0 ]
    then
      userexists=1
    else
      echo "That user does not exist!"
  fi
done
# This is the heading for the information to be displayed
echo "Information for" $username
echo "--------------------------------------------------------------------"


awk -v varname="var" -f passwd.awk /etc/passwd
awk -f shadow.awk /etc/shadow







BEGIN { FS = ":" }


/"variable"/{print "Username\t\t\t" $1
print "Password\t\t\tSet in /etc/shadow"
print "User ID\t\t\t\t"$3
print "Group ID\t\t\t"$4
print "Full Name\t\t\t"$5
print "Home Directory\t\t\t"$6
print "Shell\t\t\t\t"$7
}

I need to use the variable I get from the shell script and put it in the awk script to search the passwd file for the certain user and display the said information, but am unsure how it works. I don't full understand how to use the -v command and where to put it in the awk script.

我需要使用我从shell脚本获得的变量并将其放入awk脚本中以搜索特定用户的passwd文件并显示所述信息,但我不确定它是如何工作的。我不完全了解如何使用-v命令以及将其放在awk脚本中的位置。

3 个解决方案

#1


1

If all you need to do is pass shell variable $username to awk as awk variable varname:

如果你需要做的就是将shell变量$ username传递给awk作为awk变量varname:

awk -v varname="$username" -f passwd.awk /etc/passwd

Inside your awk program you can then refer to varname (without $), which will return the same value that $username has in the shell context.

在awk程序中,您可以引用varname(不带$),它将返回$ username在shell上下文中具有的相同值。

Since /etc/passwd is :-separated and the username is the 1st field, here's how you could match against the username field specifically:

由于/ etc / passwd是:-separated,用户名是第一个字段,以下是具体如何匹配用户名字段的方法:

awk -F: -v varname="$username" -f passwd.awk /etc/passwd

Then, inside passwd.awk, you can use the following pattern:

然后,在passwd.awk中,您可以使用以下模式:

$1 == varname 

更多相关文章

  1. linux_locale的设定中LANG、LC_ALL、LANGUAGE环境变量的区别
  2. 【Linux_Shell 脚本编程学习笔记五、Oracle JDK1.8 安装shell 脚
  3. 我已提取并尝试使用启动脚本(./start navicat)来启动 Navicat Linu
  4. Linux和Windows下查看、设置环境变量的比较
  5. 在Linux中的脚本中安装应用程序
  6. Bash脚本删除目录中多个文件名末尾的'x'字符数量?
  7. Bash脚本不删除给定目录中的文件
  8. Linux Shell脚本攻略
  9. Shell脚本创建linux用户帐户但密码出错

随机推荐

  1. sscanf函数引起android 5.0卡死,C++中慎用
  2. Android 系统基础
  3. Android之Window类简介
  4. 强烈推荐Android新手的入门学习策略之一,A
  5. Android下添加新的自定义键值和按键处理
  6. Android输入框限制字符长度,字母自动大写
  7. Android体系框架
  8. Android KitKat 4.4 Wifi移植AP模式和网
  9. [Android]判断是否是华为EMUI系统
  10. android访问网络,下载图片,并设置UI