从BDYJY到shell脚本

来源:互联网 发布:万方期刊数据库 编辑:程序博客网 时间:2024/06/03 23:46
生气本段代码实现输入用户名,输出对应的ID:
对应的操作是/etc/passwd
########################################################################## File Name: func.sh# Author: XXDK# Email: v.manstein@qq.com# Created Time: Wed 01 Mar 2017 06:30:36 PM PST##########################################################################!/bin/bashfunction plus(){a=$1b=$2sum=`expr $a + $b`return $sum}function strpls(){echo "$1 $2"}plus 2 3echo "$?"stra="xxdx 210"strb="xxdx 101"set -xstr=`strpls "$stra" "$strb"`set +xecho "$str"#定义函数 check_uaser#检查输入的用户名function check_user(){tmp=$1#获得passwd文件中的用户名列表namelist=`cat /etc/passwd | cut -d ":" -f 1`flag=0for nl in $namelistdo#如果存在if [ $1 = $nl ] thenflag=1echo "username: $1 exit"fidone#如果用户名不存在if [ $flag -eq 0 ]thenecho "username: $1 not exit, please check input"exit 0filinenum=`cat /etc/passwd | grep -n "$1:" | cut -d ":" -f 1`echo $linenumreturn $linenum}function check_line(){name=`head -$1 /etc/passwd | tail -1 | cut -d ":" -f 1,3,4`echo $name}while true doecho -n "Input a username:"read nameif [ $name = "quit" ]then echo "quit!"breakficheck_user $nameline=$?info=`check_line "$line"`echo $infodone

0 0