How to modify a file name within a shell script? - linux

I am writing a shell script to sync to a github repo, kick off the build, then take the output file, rename it, and move it to a location where it can be seen by Apache.
It's the renaming of the file that I've got not the faintest how to do within a shell script (I have virtually no experience with shell scripts - my understanding
Compiler will create /var/espbuild/firstpart_1vXX_secondpart.bin
I need to move this file to:
/var/www/html/builds/espbuild/firstpart_1vXX_DATE_secondpart_postfix.bin
1vXX is the version number
DATE is the output of date +%m-%d
postfix is just a string.
I'm not really certain where to start for something like this - I'm sure there's a graceful way, since this is the kind of thing shell scripts are made for, but I know just about nothing about shell scripts.
Thanks in advance

You can get the result of a command into a variable by using $():
DATE=$(date +%m-%d)
Then just use it in the new filename:
INPUT=/var/espbuild/firstpart_1vXX_secondpart.bin
OUTPUT=/var/www/html/builds/espbuild/firstpart_1vXX_${DATE}_secondpart_postfix.bin
mv ${INPUT} ${OUTPUT}
Edit: To get out the version part, here's a quick example:
VERSION=$(grep -o 1v.. <<< ${INPUT})
Then OUTPUT should be set like:
OUTPUT=/var/www/html/builds/espbuild/firstpart_${VERSION}_${DATE}_secondpart_postfix.bin

You can use this in BASH:
f='/var/espbuild/firstpart_1vXX_secondpart.bin'
s="${f##*/}"
s2=${s##*_}
dest="/var/www/html/builds/espbuild/${s%_*}_$(date '+%m-%d')_${s2%.*}_postfix.bin"
echo "$dest"
/var/www/html/builds/espbuild/firstpart_1vXX_07-14_secondpart_postfix.bin
cp "$f" "$dest"

Related

Bash script that would give out information about the desired directory

I need a script that would be similar to the ls and dir commands. To display information about the needed directory.
#!/bin/bash
for entry in *
do
echo "$entry"
done
But this script outputs files only in the directory where the script is located. How do I make the output in the directory that I need?
you can accept args with $1, etc. and refer to them later
for file in "$1"; do
This APP provides a better way to learn and understand unix/linux. I would like to suggest you to take a look.
https://play.google.com/store/apps/details?id=com.kanha.unixlinuxpocketbook
#!/bin/bash
for entry in /dir/I-need/*
do
echo "$entry"
done
also may be you would like
find /dir/I-need

Execute a command in a script and store the result in a file in bash

I'm trying to store the result of this command that is written in a script
ls -l /etc|wc -l
in a variable on another file.
To summarize, I have a script with that command and when I execute it, I want the result to be stored in a variable in another file.
Can someone help me with this please?
You may try to use temporary file (if possible).
This command:
ls -l /etc|wc -l > /tmp/myvar.txt
Another file:
myvar="$(cat /tmp/myvar.txt)"
You just need to use '> path/to/file' at the end of your command to redirect the output to a file (this will override the file content).
If you need another behavior, like append the content, you should use '>>' instead of '>'.
Take a look here for more details.
I'm not sure I understand what you're trying to do so I'll give you two solutions.
If the command you mention is in some file script_A.sh and you want the results of that script stored in some variable $var when running some other script script_B.sh, randomir's solution is good. In script_B:
var=$(bash path/to/script_A.sh)
If what you're asking is to run script_A.sh and then have it write a new line to a file that would store the results to a value when you run script_B.sh, I suppose you could run something like:
result=$(ls -l /etc|wc -l)
echo "var=\"$result\"" > path/to/script_B.sh
or even replace a line in a script_B.sh that already exists:
result= $(ls -l /etc|wc -l)
sed -i "s|var=SOMEPLACEHOLDER|var='$result'|" path/to/script_B.sh
If the latter is what you want, though, can you tell us more about what you're trying to accomplish? There's probably a better way than what you propose.

Running script on output files

I am trying to run script on output files that can be further used as input files for gaussian.
I wanted to know what are the commands used in Linux to run the script on .log files and .HSCP1 files.
Many thanks,
Regards,
The generic syntax of passing an argument to a script in linux, assuming your script is named script.sh and your target file is named arg.log, would be
script.sh arg.log
This assigns the name arg.log to $1 inside the environment of the executing copy of script.sh. If you don't then do something with that, it won't matter.
You might also have your script read its stdin like this:
script.sh < arg.log
which will put the contents os arg.log on script.sh's stdin, but unless it reads them accordingly, it won't matter.
Of course, both these assume script.sh is in your $PATH; otherwise you will need to apply a path for the OS, such as /path/to/dir/with/script.sh or (if you are in the same directory) ./script.sh.
If what you are asking is how to get a lorge number of files assigned as arguments, you could pass wildcards - for the first example above, that could be done as
./script.sh /path/to/*.log /also/to/other.*
or you could use find, maybe with xargs like so -
find /path/to/files/ -name *.log | xargs /path/of/script.sh
which will call the script over and over.
I hope one of these helps, but you really must provide more context for what you are doing and how.

script to change old file to timestamp

I am working on a script using #!/bin/csh -f
this script is designed to do a bunch of things but one of the things is its suppose to move file_1 to file_old and the problem is whenever you have already ran the script and there is already has a file_old it says sorry cant help ya and exits out. Is there something I can add to the script to change the old file to file_time stamp?
If you use
timestamp=\`date +%s\`
you can use append the $timestamp variable to your filename how you want it, that will give you a unique name.
mv -f file_new file_old
Use option -f.

cp command won't run if executed from shell script

i have very simple shell script
#!/bin/bash
cp -rf /var/www/ksite/app2/* /var/www/ksite/app
echo "----"
echo "done"
but seems cp command fails
if i execute
cp -rf /var/www/ksite/app2/* /var/www/ksite/app
from terminal everything work ok. Can someone tell me how to include cp in shell script?
Thanks
We seem to have doubt as to how this script fails. If there is no error message then this is a strange one. I suggest:
On the command line (which works), do a which cp
Whatever the reply, then copy that and use it as the cp in the script (e.g. /bin/cp)
Check the widcard expansion, run your script with bash -x script-name and see if you get what you expect.
echo $? after the copy in the script - if it is zero then it (thinks it) worked.
Do a ls -ld /var/www/ksite/app from your script, maybe someone set a symbolic link?
If it still fails, source the script from the command-line and see if that works . script-name
Double check that the copy did actually fail! (maybe that should be step 1.)
Make sure you really have bash at /bin/bash. I think a batter hash bang is:
#!/usr/bin/env bash
This uses the env command to locate the bash binary and set the environment.
I had similar problem. What helped me:
I used windows and putty to write script, so I had \r\n at the end of lines. Be sure, you have only \n symbol.
I copied files and the only way it worked for me at script was cp <source_dir>/fileName <dest_dir>/fileName whereas at command line cp <source_dir>/fileName <dest_dir> worked well too.
Just covering all the bases .. do the permissions vary between the excutions .. i.e. do you execute one with sudo/root privileges, the other as user (unlikely, but thought I'd ask since we don't know what the exact error is)
Similar issue to Vladmir where the script was created in Windows. I created a new file "my_bash_script.sh" in the linux environment using VIM, then read the contents of my script into the file:
:r file_made_in_windows.sh
Then I saved, closed, then set the file as executable:
chmod 744 my_bash_script.sh
From there, I ran the script:
./my_bash_script.sh
...and it worked. What a weird issue. I was confounded for a moment.

Resources