oogieoogieful oogieoogieful - 1 year ago 58
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
location="Foo, Montana "
declare -a file_array=("unique_ID_0" "unique_ID_1")


# 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
unset IFS

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

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 Source

This is much simpler than you are making it.

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


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

cat <<EOF > "$upload_file"

# ...

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download