Error found when loading /etc/profile/ - linux

When I turn on my laptop(Ubuntu 14.04),appears
Error found when loading /etc/profile:\n\n/etc/profile.d/myenv_vars.sh:line
LD command not found
What should I do? Can I delete profile.d?

Your file should have
LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
"space" is not a valid character for shell variable names.
To avoid the trailing colon:
LD_LIBRARY_PATH=/usr/local/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
If LD_LIBRARY_PATH is null or unset, the new value will be just "/usr/local/lib"
If LD_LIBRAY_PATH has a value, the new value will be "/usr/local/lib:old_value"

If the first line of the file contains:
#!/bin/sh
but the file has cr/lf line endings, you will get this error. Do this:
# dos2unix /etc/profile.d/env_vars.sh
and try logging in again:
$ exec ${SHELL} -l
Should be no errors.

Related

Linux .sh auto set local variables empty when echo

./run.sh:
1. cp=warmonger-1.0.0.jar
2. cmmd="java -server -D64 -Xms200m -Xmx200m
-Dlog4j.configurationFile=$WARMONGER_HOME/etc/log4j2.xml
-classpath $cp warmonger.agent.WarmongerAgentApp"
3. echo $cmmd
execute results:
dataq.agent.DataqAgentApp -Xmx200m
-Dlog4j.configurationFile=/warmonger/etc/log4j2.xml
-classpath warmonger-1.0.0.jar
"warmonger.agent.WarmongerAgentApp" not appear.
I means if remove echo, java will be throw an exception: Couldn't find main class
You won't see $cp when you echo $cmmd, because the shell substitutes the value of the cp parameter (warmonger-1.0.0.jar) in the assignment to cmmd.
You can escape the dollar sign, or use single quotes if you don't want the shell to expand the parameter.
Your shell script is CR-LF terminated (DOS/Windows ends of lines). Thus, from bash point of view, the cp variable contains warmonger-1.0.0.jar<CR> (notice the trailing <CR>).
When you echo the content of the cp variable, <CR> is echoed too which puts the cursor at the beginning of the line (CR = carriage return). echo then prints the remaining of the arguments at the beginning of the line.
You can see it in your output:
"java -server -D64 -Xms200m -Xmx200m" is overwritten with "warmonger.agent.WarmongerAgentApp"
which, in turn, is overwritten by some other command output ("dataq.agent.Dataq")
Solution: turn your DOS/Windows text file into a UNIX one. See this answer.

How to concatenate a string from an included file in bash

What I'm trying to accomplish is having a central configuration file, in bash, that defines some variables that are re-used in different bash files. The example below attempts to generate a file name with the current date included in the file name as well as a variable defined in another shell script. However whenever I try to concatenate this external variable it doesn't work. I can concatenate the variable in any other situation.
Example Code:
../config/vars.sh
#!/bin/bash
mysqlUser="backupuser"
mysqlPwd="fakePwd"
mysqlSocket="/var/run/mysqld/mysqld.sock"
mysqlPort="3306"
serverName="s01.catchyservername.com"
./dbBackup.sh
#!/bin/bash
source ../config/vars.sh
tempName=$(date +"%Y%m%d.sql.gz")
fileName="mysqld_${mysqlPort}_${tempName}"
echo "mysqld_${mysqlPort}"
echo ${tempName}
echo ${fileName}
output of dbBackup.sh
mysqld_3306
20140926.sql.gz
_20140926.sql.gz
As you can see when echoing "mysqld_${mysqlPort}" I get the expected output, but when echoing ${fileName} the entire first half of the string is ignored. What am I misunderstanding?
Your vars.sh file was probably created with a DOS/windows text editor:
$ ./dbBackup.sh
mysqld_3306
20140926.sql.gz
_20140926.sql.gz
$ dos2unix vars.sh
dos2unix: converting file vars.sh to Unix format ...
$
$ ./dbBackup.sh
mysqld_3306
20140926.sql.gz
mysqld_3306_20140926.sql.gz
$
As you can see above, I use the dos2unix utility to convert the line separators to Unix style.

!#/bin/bash: No such file or directory

First, I run
/bin/bash a.sh
It succeed.
But when I want to run
/bin/bash a.sh > a_info.txt
It failed.
Error: line 1: !#/bin/bash: No such file or directory
It does not seem that it’s '^M' that’s causing this error.
The shebang pattern is #!/bin/your_shell, not !#/bin/your_shell. Just fix the typo.
Check the file's encoding.
If you get this error, but you did not misspell the 'shebang', have a close look at the file's encoding. I was getting this error on a file with encoding 'UTF8 with BOM'... I guess the BOM (Byte Order Mark) was causing the problem. Changed the encoding to ASCII and that fixed it (or you can use 'UTF-8 without BOM' which is effectively the same for files only containing ASCII characters).
Another possible cause of the "No such file or directory" error is if your shell script is using CRLF instead of LF line endings.

Check if directory exists not working

I have a textfile (qrs.txt) which contains dir names (one per line) and on my server in the same directory as the script I have those folders with corresponding names from the text file.
This is my script:
#!/bin/bash
while read p; do
if [ ! -d "$p" ];
then
echo "ERROR $p" >> log.txt
else
echo "GOOD" >> log.txt
fi
done < qrs.txt
qrs.txt:
1992300000183805
1992300001176204
1992300002145500
1992300003104507
1992300004104902
1992300005133703
1992300006117802
1992300007144501
1992300008172803
1992300009189005
1992300010146307
1992300011151700
1992300012190007
1992300013126802
1992300014111508
1992300015193908
When that if statement is inside the loop it always returns error which is incorrect because I can see the folders exist. When I take it out of the loop and check for just 1, it works fine... When I echo $p on the same line as error, I can see the file name its checking is indeed correct.
What am I missing here..?
EDIT:
Screenshot of qrs.txt in hex mode:
http://i.snag.gy/25mqJ.jpg
RESOLVED!
My qrs.txt was in [dos] format originally but once converted to unix format using ":set ff=unix" the script worked like a charm!
Your script works fine.
I copied your script to my local machine. When I put blh blah in the qrs.txt file, I got ERROR for each time I ran your script. I ran it four times. I changed the blh blah to a valid path and I received GOOD.
The directory 1992300000183805 for instance, may be not be a valid path. You need the fully qualified path name! For example, /home/user/1992300000183805.
ERROR blh blah
ERROR blh blah
GOOD
GOOD
EDIT
Looking at #chepner comments, I recreated your problem:
Open your qrs.txt file in vi or vim. You should see ^M at the end of your lines. To remove the ^M characters at the end of all lines in vi, use:
:%s/^M//g
This should fix your problem. If not, in vim type this:
:set ff=unix
save the file.
Re-open qrs.txt in vim, then run the regex above again, or manually delete the ^M.
Or you can use perl:
perl -pi -e "s/\r/\n/g;" <file>
OK so looking at your provided file it seems those are relative directory names -- as such current directory is very important when you execute the script. Do you execute the script from its own directory or from the parent directory to all the (sub)directories shown in your example?
In other words have you tried:
cd <parent directory>
/path/to/yourscript.sh
?
Not to mention the location of qrs.txt seems to be specified relative rather than absolute path. So if there's no qrs.txt in the current directory I don't think your script would work.

KornShell (ksh) redirection

I have a script which redirects std out/std err as below:
SCRIPTS=/test/scripts
LOG=/test/log
echo $SCRIPTS
echo $LOG
$SCRIPTS/dmm_algo_ofac_daily_sched.ksh >> $LOG/test12.log 2>&1
This script is not able to expand $SCRIPTS and $LOG
If I replace it as below:
/test/scripts/daily_sched.ksh >> /test/log/test12.log 2>&1
It complains as below:
: bad file unit numberd/test.ksh: line 33: 1
Also I am not able to invoke the script from the directory where it is saved. If I do
./test.ksh it gives me error saying file not found. I am able to execute it via ksh /test/sched/test.ksh though.
Can someone help me with these. Thanks in advance.
I'm almost certain that the problem is because of DOS/Windows line endings
The error message you are getting is overwriting itself because of a carriage return. You can fix your file using dos2unix.
Add magic #!/bin/ksh to the first line to invoke directly without naming the interpreter on the command line.
I'll conjecture wildly that your root cause(s) has (have) nothing to do with redirection.
Is the script you've exhibited /test/sched/test.ksh or /test/scripts/test.ksh? Are you certain?

Resources