在返回/退出功能之前弹出

时间:2016-11-07 20:17:48

标签: bash

在bash中返回/退出函数之前popd的正确方法是什么?例如,

function fn () {
    pushd ~ >/dev/null
    if [ $1 = $2 ]; then
        return 0;
    elif [ $2 = $3 ]; then
        return 1;
    else
        return 2;
    fi
    popd >/dev/null
}

我应该在每次回归前写popd吗?或者,我应该采取另一种方式吗?

1 个答案:

答案 0 :(得分:7)

为此,我可能会这样做:

func showLocalNotif() {      


    if #available(iOS 10, *) {
        let content = UNMutableNotificationContent()
        content.title = "your title"
        content.subtitle = "your subtitle"
        content.body = "your message to the users"
        content.categoryIdentifier = "message"

        //Set the trigger of the notification -- here a timer.
        let trig = UNTimeIntervalNotificationTrigger(
            timeInterval: 100,
            repeats: false)

        //Set the request for the notification from the above
        let request = UNNotificationRequest(
            identifier: "100.second.message",
            content: content,
            trigger: trig

        )

        //Add the notification to the current notification center
        UNUserNotificationCenter.currentNotificationCenter().addNotificationRequest(request, withCompletionHandler: addNotificationRequestHandler)


    }}

这样我就不必在每次返回之前重复“清理”代码。

另一种方法是在子shell中完成工作并将# create the dataframe mat1 <- as.matrix(cbind(c(rep(1,10),0,1,1,1,0,rep(0,5),rep(1,7),0,0,0),c(rep(0,7),rep(1,15),rep(0,8)),c(rep(0,18),1,0,1,1,1,0,0,rep(1,5)),c(rep(0,10),rep(1,6),1,1,0,0,rep(1,6),0,0,0,1))) # use only rows satisfying your condition mat2 <- mat1[rowSums(mat1) > 1, ] # set up char matrix letterMat <- cbind(rep("a", nrow(mat2)), rep("b", nrow(mat2)), rep("c", nrow(mat2)), rep("d", nrow(mat2))) # replace values in the char matrix letterMat[mat2 == 0] <- 0 放到所需的目录中,尽管子shell无法修改父级的环境(毕竟这是我们想要的一部分)。

fn() {
    pushd ~ >/dev/null
    if [ "$1" = "$2" ]; then
        ret=0
    elif [ "$2" = "$3" ]; then
        ret=1
    else
        ret=2
    fi
    popd >/dev/null
    return $ret
}