oogieoogieful oogieoogieful - 2 months ago 7
Python Question

Modify string in bash to contain new line character?

I am using a bash script to call google-api's upload_video.py (https://developers.google.com/youtube/v3/guides/uploading_a_video )

I have a mp4 called output.mp4 which I would like to upload.

The problem is I cannot get my array to work how I would like.

This new line character is "required" because my arguments to python script contain spaces.

Here is a simplified version of my bash script:

# Operator may change these
hold=100
location="Foo, Montana "
declare -a file_array=("unique_ID_0" "unique_ID_1")

upload_file=upload_file.txt
upload_movie=output.mp4

# Hit enter at end b/c \n not recognized
upload_title=$location' - '${file_array[0]}' - Hold '$hold' Sweeps
'
upload_description='The spectrum recording was made in at '$location'.
'
# Overwrite with 1st call > else apppend >>
echo "$upload_title" > $upload_file
echo "$upload_description" >> $upload_file

# Load each line of text file into array
IFS=$'\n'
cmd_google=$(<$upload_file)
unset IFS

nn=1
for i in "${cmd_google[@]}"
do
echo "$i"
# Delete last character: \n
#i=${i[-nn]%?}
#i=${i: : -nn}
#i=${i::${#i}-nn}
i=${i%?}
#i=${i#"\n"}
#i=${i%"\n"}
echo "$i"
done

python upload_video.py --file=$upload_movie --title="${cmd_google[0]}" --description="${cmd_google[1]}"


At first I attempted to remove the new line character, but it appears that the enter or \n is not working how I would like, each line is not separate. It writes the title and description as one line.

How do I modify my bash script to recognize a newline character?

Answer

This is much simpler than you are making it.

# Operator may change these
hold=100
location="Foo, Montana"
declare -a file_array=("unique_ID_0" "unique_ID_1")

upload_file=upload_file.txt
upload_movie=output.mp4

upload_title="$location - ${file_array[0]} - Hold $hold Sweeps"
upload_description="The spectrum recording was made in at $location."

cat <<EOF > "$upload_file"
$upload_title
$upload_description
EOF

# ...

readarray -t cmd_google < "$upload_file"

python upload_video.py --file="$upload_movie" --title="${cmd_google[0]}" --description="${cmd_google[1]}"

I suspect the readarray command is all you are really looking for, since much of the above code is simply creating a file that I assume you are receiving already created.