即使退出后,bash命令仍然执行

时间:2018-09-24 04:13:14

标签: bash scripting

我遇到了一个问题,即使到达“ exit 0”,我的bash脚本仍然执行命令。

#!/bin/bash

./command | while IFS= read -r l;

do
    ./command1 | while IFS= read -r m;
        do

            if openssl enc -aes-256-cbc -a -d -in junk.txt > junk1.txt -k "${m:9:41}";
            then
                exit 0
            fi
        done
done

1 个答案:

答案 0 :(得分:0)

问题在于,管道中的shell命令(如两个 new Client { ClientId = "hybridclient", ClientName = "Example Hybrid Client", ClientSecrets = new List<Secret> { new Secret("idsrv3test".Sha256()) }, Enabled = true, Flow = Flows.Hybrid, RequireConsent = false, RedirectUris = new List<string> { hybridAppUrl,hybridApp1Url }, PostLogoutRedirectUris = new List<string> { hybridAppUrl,hybridApp1Url }, LogoutUri= hybridAppUrl+"Account/SignoutCleanup", AllowedScopes = new List<string> { Constants.StandardScopes.OpenId, Constants.StandardScopes.Profile, Constants.StandardScopes.Email, Constants.StandardScopes.Roles, Constants.StandardScopes.OfflineAccess, "sampleApi", //"id" }, AccessTokenType = AccessTokenType.Jwt, RefreshTokenUsage=TokenUsage.ReUse, RefreshTokenExpiration=TokenExpiration.Sliding } 循环)在子shell中而不是在主shell中执行。这意味着其中一个循环中的 public ActionResult SignOut() { Request.GetOwinContext().Authentication.SignOut(); return this.Redirect("/"); } [AllowAnonymous] public void SignoutCleanup(string sid) { var cp = (ClaimsPrincipal)User; var sidClaim = cp.FindFirst("sid"); if (sidClaim != null && sidClaim.Value == sid) { Request.GetOwinContext().Authentication.SignOut("Cookies"); } } 退出子Shell,而不是父Shell。 (并且在子shell中定义/更改的变量不会影响父shell,并且...)

有两种方法可以使循环在主外壳中运行并避免此问题。一种是将输出发送到临时文件,然后从中读取:

while

...但是要正确执行此操作,则需要生成唯一的临时文件名(exit是您的朋友),之后再删除它们,等等。

另一种可能性是使用bash的进程替换功能直接从其他命令重定向循环的输入:

./command >sometempfile
while IFS= read -r l; do
    ./command >anothertempfile
    while IFS= read -r m; do
        if openssl enc -aes-256-cbc -a -d -in junk.txt > junk1.txt -k "${m:9:41}"; then
            exit 0
        fi
    done <anothertempfile
done <sometempfile

像这样使用流程替换与管道非常相似,不同之处在于您可以控制子流程中的内容和不包含的内容。另外,这是仅bash功能。当它以mktempwhile IFS= read -r l; do while IFS= read -r m; do if openssl enc -aes-256-cbc -a -d -in junk.txt > junk1.txt -k "${m:9:41}"; then exit 0 fi done < <(./command) done < <(./command) 以外的任何形式调用时,在其他shell中甚至在bash中都不可用。因此,请使用显式的bash shebang(shbash),并且不要通过使用#!/bin/bash运行脚本来覆盖它。

相关问题