This question already has answers here:
How to pass a variable containing slashes to sed
(7 answers)
Closed 11 months ago.
I am trying to automate some patching steps and I have written a script to back up the file and then replace the path in the file in all spots, upon testing backing up the files was ok but the find and replace even though it states successful didn't work, I am trying to use said but I am not married to that so if there is a cleaner way I am not opposed, please see my code example below:
#!/bin/bash
#set -x
nodemanager="/u01/app/oracle/admin/domain/mserver/ADF_INT/nodemanager/"
bindirectory="/u01/app/oracle/admin/domain/mserver/ADF_INT/bin/"
ouilocation="/u01/app/oracle/product/fmw/middleware12c/oui/bin/"
date=$(date +"%d-%m-%y")
echo $date
read -p "Please enter the current jdk path: " oldjdk
read -p "Please enter the new jdk path: " newjdk
echo "Backing up an uploading files to remote server...."
cd $nodemanager || exit
cp nodemanager.properties nodemanager_$date.bkp
if [ $? -ne 0 ]; # Again checking if the last operation was successful if not shall exit the
script
then
echo -e "nodemanager.properties backup failed"
echo -e "Terminating script"
exit 0
fi
sed -i -e 's/${$oldjdk/$newjdk}/g' nodemanager.properties
if [ $? -ne 0 ]; # Again checking if the last operation was successful if not shall exit the
script
then
echo -e "find and replace failed for nodemanager.properties"
echo -e "Terminating script"
exit 0
fi
echo -e "nodemanager.properties operations completed successfully\n"
Thanks
JJ
To save you some trouble on this I figured it out the / is not part of sed it can be any delimiter that is not clashing with the path so I used this:
sed -i "s+$oldjdk+$newjdk+g" file
Thanks
JJ
Related
This question already has answers here:
Check if a file exists with a wildcard in a shell script [duplicate]
(21 answers)
Closed 3 years ago.
I want to check if file exists but only checking if some the filename exists
For example in some folder I have these files (Date format: AAAAMMDD):
Rejets_20190112.csv.zip
Rejets_20190312.csv.zip
Rejets_20190314.csv.zip
I want to check if there is a file that begins with Rejet_DAP_201903 exists in that folder. In other word I want to check if Rejet_DAP file with current year and month exist, the day doesn't matter.
Here's what I tried to do in my script:
jour=`date +%d`
mois=`date +%m`
annee=`date +%Y`
FILE="/appli/project/echanges/RTY/receptions/Rejet_${annee}${mois}"_*
if [[ -e $FILE ]]
then
echo "FILE EXISTS"
else
echo "FILE DOES NOT EXIST"
fi
You have the directory path and the file pattern that you are looking for.
The ls command can be used to list files based on patterns.
All commands return an integer value after execution. 0 means the execution finished successfully. Any non-zero value means error.
ls will return a non-zero value if no files match the pattern.
That return code you can use within the if statement.
#!/bin/bash
jour=`date +%d`
mois=`date +%m`
annee=`date +%Y`
/appli/project/echanges
dir="/appli/project/echanges/RTY/receptions"
file_prefix="Rejet_DAP_"
if ls $dir/${file_prefix}${annee}${mois}* > /dev/null 2>&1
then
echo "FILE EXISTS"
else
echo "FILE DOES NOT EXIST"
fi
The #!/bin/bash line is called a shebang line and I highly recommend using it in your scripts.
The > /dev/null 2>&1 is so that you don't get output from the ls command and only have your output displayed.
You can use find for this
$ if [[ `find /appli/project/echanges/RTY/receptions/ -type f |grep -i Rejet_DAP_${annee}${mois}|wc -l` -gt 0 ]] ; then
echo "FILE EXISTS"
else
echo "FILE DOES NOT EXIST"
fi
This question already has answers here:
How do I prompt for Yes/No/Cancel input in a Linux shell script?
(37 answers)
How do I tell if a file does not exist in Bash?
(20 answers)
Closed 5 years ago.
#!/bin/bash
filename=../deleted/$1
#Testing condition before deletion of file
if [ "$1" = "" ] ; then
echo "No filename provided"
elif [ -f "../deleted/$1" ] ; then
echo "File doesnot exist"
str=$(fgrep "$1" ../.restore.info | cut -d ":" -f2)
path=${str%/*}
mv "../deleted/$1" "${path}"
newname=$(fgrep "$1" ../.restore.info | cut -d "_" -f1)
mv -i "$1" "${newname}"
else
echo "file does not exist"
fi
----------
( I have written script to move file from the deleted folder to its original path and its working fine. But now i have to check if there is already a file with same name then it should give alert user "do u want to overwrite " if yes then overwrite if no or anything else then do not restore)
Source: http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html
if [[ -e "$newname" ]]; then
read -p "Overwrite? [y,N]" overwrite
if [[ "$overwrite" = [Y,y] ]]; then
mv -i "$1" "${newname}"
fi
fi
The -e flag will simply check if it exists. (you can use the -f as you did above or -r to see if it exists and is readable.) The read command will prompt the user with the text in between the quotes and store it in the variable. The last if will only move the file if they input Y or y. (I didn't include it but you could easily add an else if to say not moved if they select no and also an else for an invalid response should you choose in the second if.)
(Not infront of my machine so I could not test it, but I am pretty sure all my syntax is correct. Let me know if not tho.)
I am running quantum chemical calculations by providing the command molcas -f file.input. I now have need for putting the molcas -f into a script that also tails the last 100 lines of the generated file.log, for me to quickly confirm that everything finished the way it's supposed to. So I want to run the script run.sh:
#!/bin/bash
molcas -f [here read the file.input from command line]
tail -100 [here read the file.log]
The question is how I can make the script read the argument I give, and then find on its own the output file (which has the same filename, but with a different extension).
Follow-up
Say I have a bunch of numbered files file-1, file-2, ..., file-n. I would save time if I instead of running
./run.sh file-1.input file-1.log
I run
./run.sh n n
or
./run.sh n.input n.log
assuming that the actual filename and placement of the number n is given in the script. Can that be done?
With this code:
#!/bin/bash
molcas -f "$1"
tail -100 "$2"
You will need to execute the script run.sh as follows:
./run.sh file.input file.log
to be hornest I have/had no clue over molcas, so I jumed to this side to get basic understandings.
The syntax shoould look like this ...
#!/bin/bash
# waiting for input
read -p "Enter a filename (sample.txt): " FILE
# checking for existing file
if [ -e "$FILE" ]; then
read -p "Enter a command for moculas: " CMD
else
echo "Sorry, \"${FILE}\" was not found. Exit prgramm."
exit 1
fi
# I am not sure how this command works.
# maybe you have to edit this line by your self.
molcas $FILE -f "$CMD"
# checking for programm-errors
ERRNO=$?
if [ "$ERRNO" != "" ] && [ "$ERRNO" -gt 0 ]; then
echo "Molcas returned an error. Number: ${ERRNO}"
exit 1
fi
# cuts off the fileending (For example: sample.txt gets sample)
FILENAME="${FILE%.*}"
# checking other files
ERRFILE="${FILENAME}.err"
tail -n 100 $ERRFILE
LOGFILE="${FILENAME}.log"
tail -n 100 $LOGFILE
exit 0
I would have posted more, but its not clear what to do with this data.
Hope this helps a bit.
This question already has answers here:
How to check file exists via Bash script?
(2 answers)
Closed 7 years ago.
I am new to bash and I need to write an if statement within my code however I am unsure how to write is so that if $someVar is found then run this else do that. In this case $someVar is a file and I am not wanting to output text just run another line of code in this case creating the file.
Code:
rm /var/path/to/folder/$someVar
for i in `seq 3 253`
do
echo $ALLOCATION.$i >> /var/path/to/folder/$someVar
To check for the existence of /var/path/to/folder/"$someVar", then you can use:
#!/bin/bash
if [[ -f /var/path/to/folder/$someVar ]]; then
rm /var/path/to/folder/"$someVar"
fi
for i in `seq 3 253`; do
echo $ALLOCATION.$i >> /var/path/to/folder/"$someVar"
done
You can also use a simple compound-command:
#!/bin/bash
[[ -f /var/path/to/folder/$someVar ]] && rm /var/path/to/folder/"$someVar"
for i in `seq 3 253`; do
echo $ALLOCATION.$i >> /var/path/to/folder/"$someVar"
done
Note: within [[ ... ]] you do not have to quote your variable to protect against spaces in the variable. In all other cases you should (as a rule of thumb).
Also, note to check whether the file is writeable (i.e. you have permission to remove it), you can use [[ -w /var/path/to/folder/$someVar ]]
I'm trying to write a shell script that grabs a set of parameters from a text file and then performs SFTP based on those parameters. Basically, I'm taking a daily webstats log and moving it to a central location.
The issue I'm having is that the SFTP fails based on the way I am assigning variables. I have debugged and found that the while loop works correctly by echoing out the loop of variables. The error I get is that the connection is closed.
#!/bin/sh
source /home/ntadmin/webstats/bin/webstats.profile
source /home/ntadmin/webstats/bin/webstats.blogs.profile
DATE=`date +%m%d%Y`
SOURCE_FILE="`echo $WS_BC_SOURCE_FILE | sed -e 's/mmddyyyy/'$DATE'/'`"
IFS=","
while read WS_BLOG_NAME WS_BLOG_SOURCE_VAR WS_BLOG_DEST_VAR WS_BC_SERVER1;
do
#Step 1 SFTP
cd $PERL_DIR
if $PERL_DIR/sftp.pl $WS_BC_SERVER1 $WS_BC_ID $WS_BC_PW $WS_BLOG_SOURCE_VAR/$SOURCE_FILE $WS_BLOG_DEST_VAR/$SOURCE_FILE
then
echo 'SFTP complete'
else
echo 'SFTP failed!'
exit 1
fi
#Step 2 - Check that ftp was successful (that the files exist)
if [ -e $WS_BLOG_DEST_VAR/$SOURCE_FILE ]
then
echo "FTP of $WS_BLOG_SOURCE_VAR/$SOURCE_FILE from $WS_BC_SERVER1 was successful"
else
echo "FTP of $WS_BLOG_SOURCE_VAR/$SOURCE_FILE from $WS_BC_SERVER1 was not successful!"
exit 1
fi
done < blogs_array.txt
exit 0
There is not enough information to determine what was wrong, but here is a debugging method.
Try replace the actual sftp command in perl script with a debug script like this, you should be able to locate the problem quickly.
#!/usr/bin/perl
print "arguments passed to $0\n";
$i=0;
while (defined $ARGV[$i]) {
print "arg ".($i+1)." is <$ARGV[$i++]>\n"
}