比较位置参数并写入日志文件



我正在尝试在ubuntu中创建一个bash脚本,但我需要一些帮助。这是我的说明。

创建一个称为matchit的脚本,该脚本完成以下内容。

  1. 接受2个位置参数
  2. 如果参数是相同的回声,"您好,%用户名,我们有匹配!"
  3. 如果它们不匹配"这些参数不同,则%用户名%"
  4. 附加到日志文件〜/bashlog/mylog"由%用户名%执行的脚本,可变量收到的位置%parameter1%和%参数2%"

以下是我到目前为止所拥有的。任何帮助都非常感谢!

#!/bin/bash
echo "Enter Name"
read user
if [ $user = vortex ]
        then echo "Hello,%username%, we have a match!"
        else echo "These parameters are not the same, %username%"
fi
>> /home/vortex/bashlog/mylog "Script executed by %username%, variable recieved where %parameter1% and %parameter2%"

matchit脚本下方会做

#!/bin/bash
currentuser="$(whoami)"
if [ ${1:-default1} = ${2:-default2} ]
# Providing default params too to rule out empty param matches, See [1]
then
echo "Hello $currentuser, we have a match"
else
echo "Params 1 and 2 doesn't match  or empty, $currentuser"
fi
echo "Script $0 executed by $currentuser, Parameters received where ${1:-empty} and ${2:-empty}" >> ~/bashlog/mylog 
# >> for apppend

[1] - gnu shell param扩展

最新更新