script to change old file to timestamp - linux

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.

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.

Trailing questions marks in filename that is not showing up with echo

I have a short shell script that I wrote to just create backups.
#!/bin/bash
export MyBackup="MyBackup`date +%m-%d-%H:%M`"
echo $MyBackup
vi /tmp/$MyBackup.txt
rm -rf /tmp/"$MyBackup"
However, the filename that is created is something like MyBackup12-09-08:46?.txt?. The echo command returns the correct string, but the vi command creates a file with ?'s. How do I create the file without these?
Most of the issues ive lookedu p seem to talk about encoding differences, but I would think it would display incorrectly when I echoed if that was the case.
Thanks
Replace vi with touch to just create an empty file.
I ran e ++ff=unix and it seems like each one of the lines has a ^M at the end of them, so I removed them.

How to modify a file name within a shell script?

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"

running a program in linux from perl

I want to run a program from perl by using system command(or any other ways ).
system("samtools");
I think it should pass this to shell but it complains ,Can't exec "samtools" file or directory does not exist , when I run it.I have tried many other different program for example
system("velveth");
and it works properly but not this one (samtools). Is any of you facing this problem before?
I am really puzzled.
You can give the full path to that file location.
example:
system( "/usr/bin/perl -de 1");
Try putting Linux command inside `` characters. Will work as well.
Did you modify $path for samtools in the current shell manually?
Since system starts a new sub-shell to run your command, you have to append search path for samtools yourself if doesn't exist in your .bashrc. Check it by:
perl -e 'system("echo \$PATH")'
and
echo $PATH
to see if there's any difference.

Resources