zsh and parallel: How to use functions. It says command not found

早过忘川 提交于 2020-06-16 03:30:08

问题


I have a script file

filename: test_sem_zsh.sh

main() {
    echo "Happy day"
}
export -f main 
sem --id testing --fg main

I am trying to run it using zsh

$ zsh test_sem_zsh.sh 
test_sem_zsh.sh:export:4: invalid option(s)
zsh:1: command not found: main

It says two error's 1) main command not found and 2) export:4: invalid option(s)

Where as when i try with bash it works

$ sh test_sem_zsh.sh 
Happy day

So how to get this script working with zsh also


回答1:


Not certain what you are trying to do, but this may help you define a chunk of code you can run under sem without needing bash's export:

#!/bin/zsh

# Create script in /tmp
script="/tmp/tmp-$$"
cat <<EOF > "$script"
echo "Happy day"
date
EOF

sem --id testing --fg /bin/zsh "$script"

rm "$script"

This answer is conceptual... for production use, you should set up a trap to remove the script, probably use mktemp and improve security.




回答2:


sem is just a shorthand for parallel --semaphore.

You can use env_parallel:

#!/usr/bin/zsh

# activate env_parallel (if not already done)
. =env_parallel.zsh

env_parallel --semaphore --id testing --fg main

Or if your environment is too big:

#!/usr/bin/zsh

# activate env_parallel (if not already done)
. =env_parallel.zsh

env_parallel --session

main() {
    echo "Happy day"
}
env_parallel --semaphore --id testing --fg main

env_parallel --end-session


来源:https://stackoverflow.com/questions/61948652/zsh-and-parallel-how-to-use-functions-it-says-command-not-found

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!