如何使用参数从bash调用python脚本?

问题描述 投票:0回答:3

我在这里四处转转,​​可能有一个非常简单的答案。我有一个bash脚本循环显示在拇指驱动器中的视频文件,如果它找到.mp4扩展名,它将使用Youtube的python示例上传到youtube - 它可以从命令行运行。

我在bash脚本中将参数传递给python脚本调用时遇到了麻烦:

cd /home/pi/Documents
echo $PWD
for _file in  /media/pi/*/*.mp4; do
     clean=$(echo $_file|wc -m)
     if [ $clean -gt 15 ]; then
           did=$(grep "$_file" /home/pi/Documents/uploaded_videos.txt | wc -l)
          if [ $did = 0 ]; then
               #Edit this to your liking
               #echo "now uploading $_file"
               args="--file=\"${_file}\" –-title=\"${_file}\" –-description=\"show 2018\" -–keywords=\"2018,show, Winter,show 2018,\" -–category=\"28\" -–privacyStatus=\"private\""
               python yt_up.py  "$args"
              echo $_file uploaded to youtube via google developer api | logger
               echo $_file >> /home/pi/Documents/uploaded_videos.txt
         fi
     fi
done

yt_up.py脚本无法识别这些论点。我尝试了各种报价组合,但我无法让它发挥作用。

我可以将一个参数传递给脚本:

python yt_up.py --file="$_file"工作,但没有认识到添加额外的论点。

python linux bash
3个回答
0
投票

我想你只需要在与python命令相同的行中添加参数。像这样

python yt_up.py --file="${_file}" –-title="${_file}" --description="show 2018" --keywords="2018,show, Winter,show 2018," -–category="28" --privacyStatus="private"

有用


2
投票

您当前的代码中有很多反模式和误解!

cd /home/pi/Documents

为了获得最佳实践,您应该测试这是否成功。目前可能不是问题,但这样做并没有什么坏处。

echo $PWD

这里缺少引号,但这不是致命的。

for _file in  /media/pi/*/*.mp4; do
     clean=$(echo $_file|wc -m)

这不是如何计算字符串中的字符数。你应该使用clean=${#_file}代替。

     if [ $clean -gt 15 ]; then

在这里,我想你想知道全局是否匹配任何东西。那不是怎么回事。您要么使用Bash的shopt -s nullglob选项,要么使用if [ -e "$_file" ]; then来检查glob是否与实际文件匹配。

           did=$(grep "$_file" /home/pi/Documents/uploaded_videos.txt | wc -l)
          if [ $did = 0 ]; then

这就是你检查文件是否包含字符串的方法。请改用if ! grep -q "$_file" /home/pi/Documents/uploaded_videos.txt; then

               #Edit this to your liking
               #echo "now uploading $_file"
               args="--file=\"${_file}\" –-title=\"${_file}\" –-description=\"show 2018\" -–keywords=\"2018,show, Winter,show 2018,\" -–category=\"28\" -–privacyStatus=\"private\""

在这里,您对shell如何读取命令存在误解。报价删除是在变量扩展之前执行的,因此您的报价是错误的。通常你想使用一个数组!这就是Bash阵列的用途!另请注意,这里有一些奇怪的连字符。

               python yt_up.py  "$args"
              echo $_file uploaded to youtube via google developer api | logger
               echo $_file >> /home/pi/Documents/uploaded_videos.txt
         fi
     fi
done

这是一种修复错误的可能性(希望能让它发挥作用):

#!/bin/bash

# We define a variable with the path to the uploaded_videos.txt file
uploaded_videos=/home/pi/Documents/uploaded_videos.txt

# Will expand non-matching globs to nothing:
shopt -s nullglob

# cd into the directory, and exit if fails
cd /home/pi/Documents || exit

# Use the builtin pwd instead of echo "$PWD"
pwd

for file in /media/pi/*/*.mp4; do
    if ! grep -qFx "$file" "$uploaded_videos"; then
        # Define an array to contain the arguments to pass
        args=(
            --file="$file"
            --title="$file"
            --description="show 2018"
            --keywords="2018,show, Winter,show 2018,"
            --category="28"
            --privacyStatus="private"
            )
        # call your script with the fields of the array as arguments
        python yt_up.py "${args[@]}"
        echo "$file uploaded to youtube via google developer api" | logger
        echo "$file" >> "$uploaded_videos"
    fi
done

您可以通过明确检查yt_up.py是否成功来改进最后一步:

if python yt_up.py "${args[@]}"; then
    echo "$file uploaded to youtube via google developer api" | logger
    echo "$file" >> "$uploaded_videos"
else
    echo "Something wrong happened!"
fi

0
投票

你的python的args变量是一个很大的sys.argv[1],所以你可能因此而遇到麻烦。

像这样重写shell:

cd /home/pi/Documents
echo $PWD
for _file in  /media/pi/*/*.mp4; do
     clean=$(echo $_file|wc -m)
     if [ $clean -gt 15 ]; then
          did=$(grep "$_file" /home/pi/Documents/uploaded_videos.txt | wc -l)
          if [ $did = 0 ]; then
               #Edit this to your liking
               #echo "now uploading $_file"
               args=( $(echo "--file=\"${_file}\" –-title=\"${_file}\" –-description=\"show 2018\" -–keywords=\"2018,show, Winter,show 2018,\" -–category=\"28\" -–privacyStatus=\"private\"") )
               python yt_up.py  "${args[@]}"
               echo $_file uploaded to youtube via google developer api | logger
               echo $_file >> /home/pi/Documents/uploaded_videos.txt
          fi
     fi
done

现在args是数组,每个它的元素将被python读取为分离的sys.argv[i]元素。

© www.soinside.com 2019 - 2024. All rights reserved.