This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 2 years ago.
I Have a bash .sh script and I am trying to zip up yesterday's log and archive in a AWS Bucket. I need some help with getting the format of the file name correct but not having luck with it. Here is the script:
#!/bin/bash
/jobs/copytS3.sh /var/log/appname_log_"date" --date="yesterday" +'%Y%m%d'.log
Output shows following:
zip warning: name not matched: /var/log/appname_log_date
Not sure what I am missing. The name of the file should be: appname_log_20210120.log.
I am sure it's something very basic, but I'm unable to resolve. Appreciate any help with the syntax. Thanks!
Try this:
/jobs/copytS3.sh /var/log/appname_log_$(date --date=yesterday +'%Y%m%d').log
Related
This question already has answers here:
How do I pass on script arguments that contain quotes/spaces?
(2 answers)
Bash script to cd to directory with spaces in pathname
(14 answers)
Closed 24 days ago.
I have a script that appends the date and time to all files in a folder. I use the script like this...
bash append_date.sh /home/user/Documents/Podcasts/
and that will append the date to all files in the /home/user/Documents/Podcasts/ folder
Problem is that if there is a whitespace in the directory tree it fails to do anything. ie
bash append_date.sh /home/user/Documents/My Stuff/
I have tried passing the following, but that does not work
bash append_date.sh /home/user/Documents/My\ Stuff/
How do I get this script to play nice with whitespaces?
Many thanks for any help.
This question already has answers here:
Are shell scripts sensitive to encoding and line endings?
(14 answers)
Closed 1 year ago.
I am trying to write a bash script that takes 2 inputs: a list of IDs and 2) a directory. The idea is that the script will move a subset of files corresponding to the list of IDs into a a new folder.
#! /usr/bin/bash
LIST_FILE=$(cat ${1}) # List of file IDs (example entry: 1.2345)
PATH=${2} # directory name (i.e group_1)
for i in ${LIST_FILE}
do
/usr/bin/mv /tmp/project/project_data/data_all/${i}.annotated.gz /tmp/project/project_data/${PATH}/
done
The script manages to loop ok, however I get the following error for each iteration:
/usr/bin/mv: cannot stat '/tmp/project/project_data/data_all/1.2345'$'\r''.annotated.gz': No such file or directory
It looks like the file name hasn't concatenated properly and I'm not sure why. I've tried researching the problem but I'm also quite new to bash and finding it hard to grasp the concept of the stat error. I appreciate any advice and possible solutions.
Thanks everyone.
I think that the file whose name you pass as the first argument to your script is in dos format instead of unix so you are getting extra \r characters in your file names.
You could change your third line to:
LIST_FILE=$(cat ${1}|tr -d '\r')
Bobby
This question already has answers here:
Reading java .properties file from bash
(13 answers)
Closed 4 years ago.
I have a property s3Path=s3a://myBucket in a file s3-apps.properties
Currently the path to a S3 is hardcoded in my bash script:
thePath="s3a://myBucket/myApp"
I want to get the property from the file instead and concatenate it with /myApp
So I'm getting path to the file like this:
file="/apps/properties/various/s3-apps.properties"
But how do I get the property and concatenate it to construct a path?
I guess that it should be something like this but it did not work for me:
thePath="s3a://{$file.s3Path}/myApp"
Non of the answers from the "Duplicate" helped me in understanding an solving my question. But the #cody answer below was correct and helped.
If you're using GNU grep, the following would work:
$ s3path=$(grep -Po '(?<=s3Path=).+$' "$file")
$ echo $s3path
s3a://myBucket
This question already has answers here:
Script parameters in Bash
(5 answers)
Closed 6 years ago.
I've written a simple bash script, named ocropus:
#!/bin/bash
read filename path
...
And then i realized that I can't run it like this:
ocropus filename path
Instead, I need to run it like this:
ocropus
filename path
What can I do so I don't need to hit enter before my inputs? Thanks a lot!
Command line arguments are in $1, $2, etc. So do:
filename=$1
path=$2
instead of
read filename path
This question already has answers here:
Shell command to retrieve specific value using pattern
(3 answers)
Closed 8 years ago.
I have file test.txt contains the following
AA=testing
BB=help
CC=hello
How can i make a bash script that will get each value and assign to a new variable?
#!/bin/bash
var1=testing
var2=help
var3=hello
thanks for the help
First of all a = value is not correct syntax in shell. In shell the spaces are important.
When you have a valid file, you can use the eval function to evaluate that file as a string, or simply source it.