我刚开始编写脚本。 (我刚刚完成Linux LPI要点) 我有以下挑战:
创建一个新组。每个组必须具有唯一的名称。脚本必须检查以确保系统上不存在重复的组名。如果找到重复项,则需要报告错误,管理员必须尝试其他组名。
这是我的“剧本”:
#!/bin/bash
echo "Please enter a group name"
read gname
for $gname in /etc/group; do
echo "Please enter another group name"
read gname
done
groupadd $gname
echo "Group $gname has been created"
这是我收到的错误。
Please enter a group name
alexandru
test.sh: 6: test.sh: Syntax error: Bad for loop variable
答案 0 :(得分:0)
请勿尝试解析$check = ($dbh->prepare("SELECT * FROM product_list WHERE FAMILY='PARENT'"));
$check->execute();
$row = $check->fetchAll();
// execute a pdo search to find all product parents
$jsonInput = "";
foreach($row as $rows){
$jsonInput .= '"text"=>"' . $rows['PRODUCT_NAME'] . '", "value" => "' . $rows['SKU'] . '",';
}
$jsonInput = rtrim($jsonInput, ',');
//Create an iterative string which will contain the product names and skus, removing the comma at the end.
header('Content-Type: application/json');
//Set the content type to json
$optionSelect = array(
"text" => "Great! You want to find something!",
"attachments" =>array(
"text" => "Please type what you want to find",
"fallback" => "Sorry! Cant do that at the moment!",
"callback_id" => "cg_selectproduct",
"color"=> "#3AA3E3",
"attachment_type" => "default",
"actions" => array(
"name" => "cg_choice",
"text" => "Find Product",
"type" => "select",
"options" => array($jsonInput)
)
)
);
//Create and itterate the options for the selection so it's populated
print_r(json_encode($optionSelect));
//print to show json
,而是依靠/etc/group
正确执行该工作。
当您使用groupadd
并且该组已存在的名称时,它将以非零代码退出。
groupadd
所以要循环这个,直到你得到一个正确的组:
if [[ $? != 0 ]];
then
echo "Error, group already exists"
fi
答案 1 :(得分:-2)
您可能需要迭代/etc/group
的内容,请尝试以下操作
for $gname in $(cat /etc/group); do
另一方面,最好检查新组名是否也是现有组名。
答案 2 :(得分:-2)
我说它! 感谢你们!!! (Kalpa Welivitigoda和Rawkode)
echo"请输入一个组名"
读取gname
groupadd $ gname
而[$? != 0];
做
echo"输入新的组名"
读取gname
groupadd $ gname
完成了