How to jump from one directory to another directory and also display the present working directory on screen otherwise show the error? - linux

It's showing below error :
please check the destination directory

Try this:
var=( /home/divya/dir1 /home/divya/dir2 )
for dir_list in ${var[#]}
do
cd "${dir_list}"
test="$(pwd)"
if [ "${dir_list}" = "${test}" ] ; then
echo "Destination directory is ${test}"
else
echo "Check the destination directory"
fi
done

Related

Tomcat 9 not starting on linux - Catalina.sh can't use JAVA_OPTS

We have a new Tomcat 9 installed but we're having an issue when starting it.
It seems catalina.sh isn't able to work with the JAVA_OPTS, here what the log says :
/usr/local/tomcat-d-9/bin/catalina.sh line 507: -DWEBAPP-CONF-ROOT=/usr/local/webapps-conf/: No such file or directory
/usr/local/tomcat-d-9/bin/catalina.sh line 508: -Dcas.url=https://myurl.dev.com/cas/: No such file or directory
/usr/local/tomcat-d-9/bin/catalina.sh line 515: -Dapp.id=tomcat-d-9: command not found
The WEBAPP repo (line 507) actually exists so I'm confused, also why is it interpreting a url (line 508) as a dir/repo ?
These are only 3 examples of the errors in the log to illustrate the issue, but this is happening for every single JAVA_OPTS in my setenv.sh . It's either :
"no such file or directory" : but the file/directory actually exists
"no such file or directory" : but it's for a url
"command not found" : but it's not a command to run, just a parameter
I'm fairly new to this so I might be missing something obvious. Any idea what could be happening here ?
Here's my setenv.sh
#!/bin/bash
COMMON="/usr/local/etc/BashStartFunctions.bash"
[ ! -e "$COMMON" ] && echo "Missing $COMMON" && exit 1
source $COMMON
checkUsr
fetchAppEnv
echo "PRG=$1"
JAVA_OPTS="${JAVA_OPTS_EXTRA}
-Dspring.profiles.active=dev
-Djava.library.path=/usr/lib64:/usr/local/tomcat-apr-connector/lib:/lib64
-DWEBAPP-CONF-ROOT=${WEBAPP_CONF_ROOT}
-Dcas.url=${CAS_URL}
-Dserver.url=${SERVER_URL}
-Dcom.vmd.xmltcp.client.XmltcpClient.reuseClientSocket=false
-Dcom.aubesoft.jdbc.recovery.ChainedJdbcExecutionWithRecoveryHelper.overrideChainProcessorRecoveryFile=${overrideChainProcessorRecoveryFile}
-Dcom.aubesoft.jdbc.recovery.ChainedJdbcExecutionWithRecoveryHelper.overrideInChainRecoveryFile=${overrideInChainRecoveryFile}
-Dcom.aubesoft.jdbc.recovery.RecoveryPolicy.overrideDefaultRecoveryFile=${overrideDefaultRecoveryFile}
-Djavax.net.ssl.trustStore=${TRUSTSTORE}
-Dapp.id=${APP_ID}
-Dserver.xml.serverPort=${SERVER_PORT}
-Djava.awt.headless=true"
if [ "$1" = "start" ];then
JAVA_OPTS="${JAVA_OPTS}
-Denv=${APP_ENV}
-Dtomcat.hosts=${TOMCAT_HOSTS}
-Dstore.url=${store_url}
-Dlog.dir=${LOG_DIR}
-Dserver.xml.httpPort=${HTTP_PORT}
-Dserver.xml.ajp13Port=${AJP13_PORT}
-Dserver.xml.ajp13.maxThreads=${AJP13_MAX_THREADS}
-Dserver.xml.httpsPort=${HTTPS_PORT}
-Dserver.xml.https.maxThreads=${HTTPS_MAX_THREADS}
-Dserver.xml.https.sslCertFile=${HTTPS_CERT_FILE}
-Dserver.xml.https.sslCertKeyFile=${HTTPS_CERT_KEY_FILE}
-Dserver.xml.https.sslKeystoreFile=${HTTPS_KEYSTORE_FILE}
-Dserver.xml.https.sslKeystorePass=${HTTPS_KEYSTORE_PASS}
-Dserver.xml.https.sslKeyAlias=${HTTPS_KEYSTORE_KEY_ALIAS}
-Dserver.xml.jvmRoute=${HOSTNAME}.${AJP13_PORT}
-Dserver.xml.maxHeaderSize=${MAX_HEADER_SIZE}
-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=${JMX_PORT}
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false
-Dscoperta.zone=${SCOPERTA_ZONE}
-Dscoperta.env=${APP_ENV}
-Dhttps.protocols=TLSv1
-Dsmartd.authentication.signing.secret.file=${SMARTD_AUTHENTICATION_SIGNING_SECRET_FILE}
-Dspring.profiles.active=${SMARTD_SPRING_PROFILES}
"
if [ ! -z ${CAS_SECURE} ]; then
# CAS args
JAVA_OPTS="${JAVA_OPTS} -Djava.security.auth.login.config=${JAAS_CONFIG}
-Djava.security.krb5.kdc=${KRB5_KDC}
-Djava.security.krb5.realm=${KRB5_REALM}
-Dcas.TGTTimeoutMS=${CAS_TGT_TIMEOUT_MS}
-Dcas.secure=${CAS_SECURE}"
fi
fi
CATALINA_OUT=/usr/local/tomcat-d-9/logs/catalina_${APP_ID}.out
echo "JAVA_OPTS=$JAVA_OPTS"
You're splitting up your settings into multiple lines:
-DWEBAPP-CONF-ROOT=/usr/local/webapps-conf/: No such file or directory
does not refer to /usr/local/webapps-conf/ not existing, but -DWEBAPP-CONF-ROOT=/usr/local/webapps-conf/ not existing (obviously)
You can escape the linebreaks using a \, e.g.
JAVA_OPTS="${JAVA_OPTS} \
-Denv=${APP_ENV} \
-Dtomcat.hosts=${TOMCAT_HOSTS} \
....
or
JAVA_OPTS="${JAVA_OPTS} -Denv=${APP_ENV}"
JAVA_OPTS="${JAVA_OPTS} -Dtomcat.hosts=${TOMCAT_HOSTS}"
....
or just write them all in a single line.
Also, consider to use CATALINA_OPTS, rather than JAVA_OPTS.

Bash script Output to file fails from script but works from bash

I have this script that checks if the dcos auth login works, but the file i am redirecting the output to is always zero size, when i run the script from bash shell the file is greater than zero . what am i doing wrong ?? , the two functions i use below:
try_to_login()
{
# first needs to be logged as skyusr
# try to login and log the result to tmp file
# Sometimes the file is empty so we try again to login
# if the second time is OK it jumps to check the output
cd /home/skyusr/scripts/
dcos auth login --username=admin --password=admin > /home/skyusr/scripts/tmp.sal
}
check_login_result()
{
# Checks if the output of the login is "Login Successful!"
# If YES then writes to log file, if not sends mail and writes to log.
#export mail_to="salim.bisharat#amdocs.com,anis.faraj#amdocs.com"
export mail_to="salim.bisharat#amdocs.com"
now=$(date)
text_to_check=$(cat /home/skyusr/scripts/tmp.sal)
if [ -s /home/skyusr/scripts/tmp.sal ]
then
if [ "$text_to_check" = "Login successful!" ]
then
echo "$now - Check Successful" >> /home/skyusr/scripts/logs/login_log.log
else
cat /home/skyusr/scripts/logs/mail_temp.log | mailx -s "!!! CRITITCAL -- Check DCOS login !!!" $mail_to
echo "$now - !! ERROR ! Sent mail !! " >> /home/skyusr/scripts/logs/login_log.log
fi
fi
}
In this script you define, but you do not call the functions. Simply append function calls:
# ...
echo "$now - !! ERROR ! Sent mail !! " >> /home/skyusr/scripts/logs/login_log.log
fi
fi
} # ... the last line of your script here
try_to_login # calls here ...
check_login_result

bash: How to transfer/copy only the file names to separate similar files?

I've some files in a folder A which are named like that:
001_file.xyz
002_file.xyz
003_file.xyz
in a separate folder B I've files like this:
001_FILE_somerandomtext.zyx
002_FILE_somerandomtext.zyx
003_FILE_somerandomtext.zyx
Now I want to rename, if possible, with just a command line in the bash all the files in folder B with the file names in folder A. The file extension must stay different.
There is exactly the same amount of files in each folder A and B and they both have the same order due to numbering.
I'm a total noob, but I hope some easy answer for the problem will show up.
Thanks in advance!
ZVLKX
*Example edited for clarification
An implementation might look a bit like this:
renameFromDir() {
useNamesFromDir=$1
forFilesFromDir=$2
for f in "$forFilesFromDir"/*; do
# Put original extension in $f_ext
f_ext=${f##*.}
# Put number in $f_num
f_num=${f##*/}; f_num=${f_num%%_*}
# look for a file in directory B with same number
set -- "$useNamesFromDir"/"${f_num}"_*.*
[[ $1 && -e $1 ]] || {
echo "Could not find file number $f_num in $dirB" >&2
continue
}
(( $# > 1 )) && {
# there's more than one file with the same number; write an error
echo "Found more than one file with number $f_num in $dirB" >&2
printf ' - %q\n' "$#" >&2
continue
}
# extract the parts of our destination filename we want to keep
destName=${1##*/} # remove everything up to the last /
destName=${destName%.*} # and past the last .
# write the command we would run to stdout
printf '%q ' mv "$f" "$forFilesFromDir/$destName.$f_ext"; printf '\n'
## or uncomment this to actually run the command
# mv "$f" "$forFilesFromDir/$destName.$f_ext"
done
}
Now, how would we test this?
mkdir -p A B
touch A/00{1,2,3}_file.xyz B/00{1,2,3}_FILE_somerandomtext.zyx
renameFromDir A B
Given that, the output is:
mv B/001_FILE_somerandomtext.zyx B/001_file.zyx
mv B/002_FILE_somerandomtext.zyx B/002_file.zyx
mv B/003_FILE_somerandomtext.zyx B/003_file.zyx
Sorry if this isn't helpful, but I had fun writing it.
This renames items in folder B to the names in folder A, preserving the extension of B.
A_DIR="./A"
A_FILE_EXT=".xyz"
B_DIR="./B"
B_FILE_EXT=".zyx"
FILES_IN_A=`find $A_DIR -type f -name *$A_FILE_EXT`
FILES_IN_B=`find $B_DIR -type f -name *$B_FILE_EXT`
for A_FILE in $FILES_IN_A
do
A_BASE_FILE=`basename $A_FILE`
A_FILE_NUMBER=(${A_BASE_FILE//_/ })
A_FILE_WITHOUT_EXTENSION=(${A_BASE_FILE//./ })
for B_FILE in $FILES_IN_B
do
B_BASE_FILE=`basename $B_FILE`
B_FILE_NUMBER=(${B_BASE_FILE//_/ })
if [ ${A_FILE_NUMBER[0]} == ${B_FILE_NUMBER[0]} ]; then
mv $B_FILE $B_DIR/$A_FILE_WITHOUT_EXTENSION$B_FILE_EXT
break
fi
done
done

Why does script not recognize file extension?

My script
#!/bin/bash
cp *.ats /home/milenko/procmt
mycd() {
cd /home/milenko/procmt
}
mycd
EXT=ats
for i in *; do
if [ "${i}" != "${i%.${EXT}}" ];then
./tsmp -ascii i
fi
done
But
milenko#milenko-HP-Compaq-6830s:~/Serra do Mel/MT06/meas_2016-07-13_20-22-00$ bash k1.sh
./tsmp: handling 1 files ************************************** total input channels: 1
the name of your file does not end with ats ... might crash soon
main (no rda) -> can not open i for input, exit
./tsmp: handling 1 files ************************************** total input channels: 1
the name of your file does not end with ats ... might crash soon
main (no rda) -> can not open i for input, exit
When I go to procmt directory and list files
milenko#milenko-HP-Compaq-6830s:~/procmt$ ls *.ats
262_V01_C00_R000_TEx_BL_2048H.ats 262_V01_C00_R086_TEx_BL_4096H.ats 262_V01_C02_R000_THx_BL_2048H.ats
262_V01_C00_R000_TEx_BL_4096H.ats 262_V01_C01_R000_TEy_BL_2048H.ats 262_V01_C03_R000_THy_BL_2048H.ats
What is wrong with my script?
If I understand correctly this should work for you:
dest='/home/milenko/procmt'
cp *.ats "$dest"
cd "$dest"
for i in *.ats; do
./tsmp -ascii "$i"
done
There is no need to loop through all files when you're only interested in .ats files. Your mycd function is just doing cd so you can avoid that as well.

Twice Bash command substitution

I have directories a1..a5, b1..b5 and c1..c5. Inside each directory I have two files a1, b1 and c1.
do mkdir /tmp/{a,b}$d; touch /tmp/{a,b,c}$d/{a,b,c}1; done;
I want to get all the files starting with 'a' or 'b' inside the directories starting with an 'a'. I can do it with:
DIRS=`ls -1 -d /tmp/{a,b}*/a*`
echo ${DIRS}
and obtain:
/tmp/a1/a1 /tmp/a2/a1 /tmp/a3/a1 /tmp/a4/a1 /tmp/a5/a1
/tmp/b1/a1 /tmp/b2/a1 /tmp/b3/a1 /tmp/b4/a1 /tmp/b5/a1
Now, I will use a variable called DATA to store the directories and later get the files:
DATA="/tmp/{a,b}*"
echo ${DATA}
DIRS=`ls -1 -d ${DATA}/a*`
echo ${DIRS}
In the output, the DATA contents is OK (/tmp/{a,b}*), but I receive the following error:
ls: cannot access /tmp/{a,b}*/a*: No such file or directory
Any idea why this happens?
I solved the problem, but I can't find any reference about why my previous attempts failed.
DATA="/tmp/{a,b}*"
echo ${DATA}
DIRS=`eval "ls -1 -d ${DATA}/a*"`
echo ${DIRS}
Output:
/tmp/a1/a1 /tmp/a2/a1 /tmp/a3/a1 /tmp/a4/a1 /tmp/a5/a1 /tmp/b1/a1
/tmp/b2/a1 /tmp/b3/a1 /tmp/b4/a1 /tmp/b5/a1

Resources