How to create and write a properties file using shell script - shell

I have a shell file and in that I need to create and write a content as below into my abc.properties file.
version=123456-> This is the one thing which is required in my properties file.
I tried with the below commands and its not writing the contents into the file.
sh "touch abc.properties"
sh "lscm show lastmod . -f yyyyMMddHHmmssSSS >> ${lscm_home}/abc.properties"
Can someone provide some inputs on how can i make the second command to write into the required file ? Thanks

You can try like below
echo "Version=`lscm show lastmod . -f yyyyMMddHHmmssSSS`" >> FILE_NAME

Related

Is there a way to simulate Save and Close a file through shell command?

I have a scenario where a shell script runs another shell script. The second shell script prompts user to type review comments and close the file.
In my case, I do not want to type anything but only save and quit (equivalent vim command :wq) the file. I want to automate this through a shell script where I don't want manual intervention to save and quit the file. How can i achieve this?
I'm not sure i get the context but if you simply (echo "input of your choice" >> file.txt ) it will do the job and will not have to quit and save anything
if th einput is already in a file you can (cat inputfile.txt >> file.txt )
this command can be place inside of your first script

Variable for a right-clicked item (say, a jpg) in a bash script?

I have a very simple bash script that I run often from the cli, but I've found it's frustrating to have to open a terminal, identify the right file, and run it and think the easiest way would be to run it as an option from a right-click. I am running Ubuntu 18.04 LTS.
The script is just erasing exif data, leaving the orientation tags, essentially this:
exiftool -all= -tagsfromfile # -Orientation file-*.jpg
Is there a way to have the script identify which image I'm right clicking on? I'm at a loss what to put in the file-*.jpg part which will be a variable for "whatever image I'm right-clicking on right now."
Tried searching for a good while on how to do this but am clearly either not using the right search terms or else this isn't done very often. Thank you in advance for any help!
if you want your script to run in file manager right-click menu you have to change your script and define file(s) as arguments. this happens simply by changing your file section with $1 to $n as the parameter(s).
as far as I know ubuntu uses nautilus as an file manager.
you can run nautilus-actions-config-tool either from your terminal or from dash and define your script a name and a command to run. you can follow this link for illustration learning :
ubuntu nautilus defile script in menu
for example :
#!/bin/bash
if [ "$1" != "" ]; then
echo "Positional parameter 1 contains value $1"
else
echo "Positional parameter 1 is empty"
fi
for all arguments :
#!/bin/bash
if [[ "$#" -gt 0 ]]; then
for arg in "$#"; do
echo $arg
done
fi
here is the image that shows the script worked
I know the question is a little older, but I can provide you with the solution.
You have to set up FileManager-actions, an extension for GNOME/Nautilus (but it also works for other file managers).
Setup filemanager-actions on Ubuntu 20.04
sudo apt update
sudo apt install filemanager-actions
Then run fma-config-tool and create a new action.
When creating an action, please ensure that:
[v] Display item in selection context menu
is flagged; otherwise, you will not see the context menu during the file selection.
Prepare the script you want to execute
Prepare a script that does what you need. Touch in /tmp, mv it in /usr/bin and give it execute permissions:
touch /tmp/your-script
# edit it with your editor
sudo mv /tmp/your-script /usr/bin/
sudo chmod +x /usr/bin/your-script
In your script, you can reference the filename using $1.
FILENAME=$1
echo $FILENAME
In the variable FILENAME you will find the selected file name.
Configure Nautilus-action command
To let nautilus pass the filename, insert the script path and the argument string in the' command' tab.
To fully answer your question, to let Nautilus pass the filename as a script argument, you have to specify %f.
At this point, quit the Nautilus instance and open it again:
nautilus -q
nautilus
Now, let's have a try! Right-click on a file and check out the result!
Appendix 1 - Filemanager-actions formats
%f A single file name, even if multiple files are selected.
%F A list of files. Each file is passed as a separate argument to the executable program.
%u A single URL. Local files may either be passed as file: URLs or as file path.
%U A list of URLs. Each URL is passed as a separate argument to the executable program.
%d Base directory
Here you can find a comprehensive list.
Appendix 2 - Sources
Check out my post blog in which I actually realize something similar: https://gabrieleserra.ml/blog/2021-08-14-filemanager-actions-ubuntu-20-04.html
Reference to all possible formats for FileManager-actions: https://askubuntu.com/a/783313/940068
Realize it in Ubuntu 18.04: https://askubuntu.com/a/77285/940068

Run curl command on each line of a file and download the files

I have a file containing a list of pdf files.
this is a test.pdf
this is a/another test.pdf
test number 5_ example.pdf
...
I know that doing:
curl -O "https://report2018/this is another test.pdf"
will download the file.
So the scenario is use curl in bash script to download all the pdf's in the file one by one when the beginning of the URL should be: "https://report2018/" .
So a complete URL will be https://report2018/+PDF_NAME
Any ideas or suggestions how to do it in bash script?
It is a pretty basic script actually...
You can break your problem in two pieces:
Basic usage of bash, in general;
How to cycle a file.
Something like this will suffice:
#!/bin/bash
exec 3<file.list # Put the file in a file descriptor
while read -r line <&3; do # Read line by line the file descriptor
curl -sk "https://report2018/${line}" > "${line}" # make curl and save in file
done
exec 3>&- # Close file descriptor
Obviously you have to change the curl with your needs (E.G. User Agent and/or authentication).
Note that with > ${line}" after curl, you will save the file with the same name read by file.list.
I hope this helps you, and please, next time, use a search engine first.

Copying .jpg file using shell script gives 'Failed to open input stream for file' error

Very simple script to copy a file
#!/bin/bash
#copy file
mtp-getfile "6" test2.jpg
I set it as executable and run it using
sudo sh ./test.sh
It gives me a file called test2.jpg that has no icon and I cannot open I get a 'Failed to open input stream for file' error
However, if I simply issue the following from the command line
mtp-getfile "6" test2.jpg
It works as expected. What is wrong with my script? I checked and the resulting .jpg file in each case has the same number of bytes. Very strange.
As commented by chepner, your file might have a DOS (Windows) invisible line ending on its name, which would cause an error. To get rid of this unwanted character(s), just create a new blank script on your "nix" system and type the name by hand (not by copying and pasting, to avoid problems), let's say, name it test2.sh.
Then copy all the contents of test.sh to test2.sh (copy and paste) and run test2.sh and see if it works. If it doesn't, try running the following code on the new script, to make sure that there are no unwanted characters on the code itself:
tr -d "\r" < /folder/test2.sh && echo >> /folder/test2.sh
And then try to run script2.sh again to see if it works. Note: the echo >> /folder/test2.sh part of the code above is just to make sure that your new script ends with a newline, which is a Posix standard (and without which some programs may misbehave because they expect the file to end with a newline).
Apparently it was a permissions issue.
I only had to do a sudu chown test2.jpg

MATLAB Bash script

Let's say I have this MATLAB function
function a = testfun(filename)
% do something with file contents and create some variable a
disp(a);
I would like to have a shell script that runs on Cygwin as follows:
./testscript.sh < inputforfunction.txt > outputoffunction.txt
The input file would contain the data I need.
After running this command, the output file will contain the result of running testfun(filename).
Up till now, I can write the output to the file outputoffunction.txt.
The problem is I want to read the file name "inputforfunction.txt".
I am able to read in the file contents but not the file name, any hints please?
Thanks!
Why not pass the file as an argument to the bash script?
./testscript.sh inputforfunction.txt > outputoffunction.txt
In the script you can access $1 - it will evaluate to 'inputforfunction.txt'.
read file_name
./testscript.sh < $file_name > outputoffunction.txt

Resources