此问题与 bash 脚本中的Ignore sudo不重复
我正在制作一个基于 Ubuntu 14.04 docker 镜像的 docker 镜像。如您所知,root 是 docker 中的默认用户。当我运行 bash 脚本来安装软件时,得到
sudo: error in /etc/sudo.conf, line 0 while loading plugin `sudoers_policy'
sudo: /usr/lib/sudo/sudoers.so must be owned by uid 0
sudo: fatal error, unable to load plugins
Run Code Online (Sandbox Code Playgroud)
我试过了chown root /usr/lib/sudo/sudoers.so,但它不起作用。
这种行为很奇怪:
ls (everyone can do this)
sudo ls (you can't do this even if you are root)
Run Code Online (Sandbox Code Playgroud)
因为我正在构建一个docker镜像,所以我不想安装不相关的包来让sudo支持root。但是sudo在一些 bash 脚本中有一堆,所以简单地替换sudo为空可能不是一个好的选择。
所以我想知道是否有什么方法可以只sudo在你是 root 时忽略
某些终端命令只能以 root 用户身份运行。如果我们尝试在没有sudo关键字的情况下执行它们,它们会因Permission denied错误而失败。一个例子是在某些位置创建目录/文件夹的命令。
我需要在我的 Ubuntu 16.04 中运行一个这样的命令作为 cron 作业的一部分。通常我可以sudo在终端中使用关键字运行命令,然后会提示我输入root用户的密码,然后命令将成功执行。
但是现在我必须在我的crontab文件中输入此命令作为 cron 作业的一部分,我该怎么做?如何将这样的命令作为 cronjob 的一部分运行?
我想在一行中打印两个变量。我正在使用 shell 脚本#!/bin/sh循环,我想要做的是for重复打印出如下内容:
variable1_case1
variable2_case2
Run Code Online (Sandbox Code Playgroud)
我已经试过了
variable$i_case$i.
Run Code Online (Sandbox Code Playgroud) 我有包含不同类型文本格式的文件,我的目标是仅提取 HTML 部分并使用此 HTML 代码创建一个文件。我认为使用grepor是可能的awk。我的文件还包含这样的行:
Sender name `<test@email.com>`
Run Code Online (Sandbox Code Playgroud)
我写了这个脚本cat file1.html | grep -E "<[^>]*>"。但问题是它也将行输出为Sender name等。我只想在<html>标签之后提取内容。所以这对我没有用:
Return-Path: <test@test.com>
for <test@localhost> (single-drop); Thu, 21 Sep 2017 18:34:07 +0400 (+04)
Return-path: <test@test.com>
(envelope-from <test@test.com>)
References: <test@test.com>
From: test user <test@test.com>
X-Forwarded-Message-Id: <test@test.com>
Message-ID: <test@test.com>
In-Reply-To: <test@test.com>
Run Code Online (Sandbox Code Playgroud) 使用 Ubuntu 16。我在cron.daily文件夹中有以下脚本。但是,我通过电子邮件通知收到错误消息:
/etc/cron.daily/clamscan_daily:
run-parts: failed to exec /etc/cron.daily/clamscan_daily: Exec format error
run-parts: /etc/cron.daily/clamscan_daily exited with return code 1
Run Code Online (Sandbox Code Playgroud)
脚本
#!/bin/bash
LOGFILE="/var/log/clamav/clamav-$(date +'%Y-%m-%d').log";
EMAIL_MSG="Please see the log file attached.";
EMAIL_FROM="no-reply@domain.com";
EMAIL_TO="admin@domain.com";
DIRTOSCAN="/var/www";
for S in ${DIRTOSCAN}; do
DIRSIZE=$(du -sh "$S" 2>/dev/null | cut -f1);
echo "Starting a daily scan of "$S" directory.
Amount of data to be scanned is "$DIRSIZE".";
clamscan -ri "$S" >> "$LOGFILE";
# get the value of "Infected lines"
MALWARE=$(tail "$LOGFILE"|grep Infected|cut -d" " -f3); …Run Code Online (Sandbox Code Playgroud) 我有一个来自 curl 响应的正常组织回购列表,命令如下:
curl "https://api.github.com/orgs/[organization]/repos?access_token=[access_token]"
Run Code Online (Sandbox Code Playgroud)
这个命令的输出是巨大的。我想提取每个 repo 的clone_url参数,遍历这些 URL 并git clone在其上运行。
下面是JSON输出curl给我的一个条目的一瞥:
[
{
"id": 42059877,
"name": "customrepo",
"full_name": "SampleOrg/customrepo",
"owner": {
"login": "SampleOrg",
"id": 14164701,
"avatar_url": "https://avatars1.githubusercontent.com/u/14164701?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/SampleOrg",
"html_url": "https://github.com/SampleOrg",
"followers_url": "https://api.github.com/users/SampleOrg/followers",
"following_url": "https://api.github.com/users/SampleOrg/following{/other_user}",
"gists_url": "https://api.github.com/users/SampleOrg/gists{/gist_id}",
"starred_url": "https://api.github.com/users/SampleOrg/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/SampleOrg/subscriptions",
"organizations_url": "https://api.github.com/users/SampleOrg/orgs",
"repos_url": "https://api.github.com/users/SampleOrg/repos",
"events_url": "https://api.github.com/users/SampleOrg/events{/privacy}",
"received_events_url": "https://api.github.com/users/SampleOrg/received_events",
"type": "Organization",
"site_admin": false
},
"private": true,
"html_url": "https://github.com/SampleOrg/customrepo",
"description": null,
"fork": false,
"url": "https://api.github.com/repos/SampleOrg/customrepo",
"forks_url": "https://api.github.com/repos/SampleOrg/customrepo/forks",
"keys_url": "https://api.github.com/repos/SampleOrg/customrepo/keys{/key_id}",
"collaborators_url": …Run Code Online (Sandbox Code Playgroud) 我想编写一个Linux脚本来从网站下载不同日期的图片。我想我需要 YY、MM 和 DD 三个循环。YY 应该是 1974-2017,MM 01-12 和 DD 01-31。它在一个小的 Linux bash 脚本中看起来如何?
这是一个简单的shell脚本示例:
#!/usr/bin/env bash
if [ 1 == 1 ]; then
echo "Something"
fi
Run Code Online (Sandbox Code Playgroud)
当我运行这个
sh ./test.sh
Run Code Online (Sandbox Code Playgroud)
我得到:
./test.sh: 4: ./test.sh: Syntax error: "fi" unexpected (expecting "then")
Run Code Online (Sandbox Code Playgroud)
它似乎无法识别我在那里的“然后”。关于为什么的任何线索?如果这部分很重要,这是适用于 Windows 的 Ubuntu。
运行 shell 脚本一般不是问题,它似乎只是在使用 if/else。
command-line bash scripts windows-10 windows-subsystem-for-linux
我刚刚开始编写 shell 脚本,尝试执行以下脚本时出错:
我在script.sh文件中有以下脚本
echo “enter a value”
read n
s=0
i=0
while [ $i –le $n ]
do
if [ `expr $i%2` -eq 0 ]
then
s= `expr $s + $i `
fi
i= `expr $i + 1`
done
echo “sum of n even numbers”
echo $s
Run Code Online (Sandbox Code Playgroud)
脚本输出:
akhil@akhil-Inspiron-5559:~/Desktop/temp$ chmod 755 script.sh
akhil@akhil-Inspiron-5559:~/Desktop/temp$ ./script.sh
“enter a value”
3
./script.sh: line 5: [: –le: binary operator expected
“sum of n even numbers”
0
Run Code Online (Sandbox Code Playgroud)
我得到的错误的根源是什么?
我在文件中有以下几行:
Modified folders: html/project1/old/dev/vendor/symfony/yaml/Tests/bla.yml
Modified folders: html/port5/.DS_Store
Modified folders: html/trap/dev8/.DS_Store
Modified folders: html/bla3/test/appl/.DS_Store
Modified folders: html/bla4/pro1/app/bla/Api2.php
Modified folders: html/bla10/dev/appl/language/.DS_Store
Modified folders: html/bla11/dev/appl/language/abc.txt
Run Code Online (Sandbox Code Playgroud)
这基本上是rsync. 我想列出文件的所有行,最多 3 个目录位置,例如
Modified folders: html/project1/old
Modified folders: html/port5
Modified folders: html/trap/dev8
Modified folders: html/bla3/test
Modified folders: html/bla4/pro1
Modified folders: html/bla10/dev
Modified folders: html/bla11/dev
Run Code Online (Sandbox Code Playgroud)
任何人都可以向我提供任何命令或 shell 脚本来做同样的事情吗?
scripts ×10
bash ×8
command-line ×5
cron ×2
permissions ×2
awk ×1
cut-command ×1
git ×1
github ×1
grep ×1
root ×1
rsync ×1
sh ×1
sudo ×1
windows-10 ×1