echo to file - error: cannot create - unix

In Unix ksh. I am trying to create a text file with few lines of text in it using below echo statement
echo $multilinetext > ../in/log_file
The error i am getting is "Cannot Create". Also this code worked fine before some time.
Through filezilla i am able to create the file. The "in" directory has all permissions(777).
the Dir hierarchy is as below
ParentDir
in
Bin
test.ksh
test.ksh is executing the echo command
What else should be causing the error
Update:
The problem was that i executed the script from parentDir. So instead of saying the file does not exist, it found the test.ksh file in Bin and executed it without any errors. which caused it to look for "in" directory in wrong location
Is it possible to check if the script is executed from its own directory?

Per your request, here's a small script based on what you've posted about your hierarchy:
#!/bin/sh -
# adjust this to suit
bin_dir="/tmp/top/bin"
multi=`cat /etc/passwd`
# get current directory
pwd="`pwd`"
# check to ensure we're in the correct directory
if [ "$pwd" != "$bin_dir" ]; then
echo "not executing from correct bin directory"
exit
fi
target_dir="../in"
# ensure the target directory for writing exists
if [ -d "$target_dir" ]; then
echo $multi > $target_dir/log_file
else
echo "unable to locate directory '$target_dir' -- does not exist"
fi

Related

What does `source` do when given a directory?

I often happen to run a command like source mydirectory, where mydirectory is a ...directory, and not a shell script file (I guess I'm not that good at predicting autocompletion :p)
What puzzles me is that zsh does not complain, and returns 0 as status code; although that does not seem a normal call to source AFAIK.
# in zsh shell
# create an empty dir
$ mkdir /tmp/mydir
# "source" it
$ source /tmp/mydir && echo "success"
# output: 'success'
Meanwhile, bash will complain when asked to "source"
# in bash
$ source /tmp/mydir/ && echo "success"
# output: 'bash: source: /tmp/mydir/: is a directory'
Does someone know why this is a valid call in ZSH?
References I looked in
SO itself
the documentation of ZSH builtin commands https://zsh.sourceforge.io/Doc/Release/Shell-Builtin-Commands.html
multiple tutorials about source
... to no avail

error compressing netcdf files using nccopy through a loop

I am trying to compress multiple netcdf files in different folders through a loop.
for i in `find . -iname 'wrfout*'`;
do
echo $i
time nccopy -d5 -s $i d_${i}
When I run this code, I get an error message
"./wrfout_d04_2011-08-13_00:00:00
Permission denied
Location: file ../../ncdump/nccopy.c; line 1429".
However, when I run the last line in the loop for individual files, it runs without any error messages. I have checked the permission mode for the execution file to "777" just to be sure, but still the error persists. Any help with this is approciated.
Your bash syntax suggests your output filenames are going to look like 'd_./wrfout_d04_2011-08-13_00:00:00'
Suggestion:
nccopy -d5 -s $i $(dirname $i)/d_$(basename $i)

add latest R installation path from registry to PATH windows 7/8/10

Hi I'm new to windows batch.
I want to hand out a runMe.bat file to co-workers calling Rscript myRfile.R to process some data files. But my co-workers have notoriously installed R various places and I cannot expect them to know how to add Rscript to PATH or even to code in R.
I would like the .bat file to lookup path of latest installed R and add [that directory]\bin\i386\ to PATH temporarily.
I imagine to:
iterate the subfolders of registry HKEY_LOCAL_MACHINE\Software\Rcore\R\ to find the last and latest R-version folder
in this registry subdirectory get the **installPath** e.g. keyValue = "c:\R\R-3.2.2\"
concatenate with "\bin\i386\" -> c:\R\R-3.2.2\bin\i386\ ->Rpath
PATH%PATH%;Rpath
Rscript myRfile.R
I prefer that the Rpath is not permanently added to PATH. My co-workers probably have quite restricted windows administrator privileges anyway.
Thank you very much!
Bonus: My company mainly has 32bit Windows OS installations, but will upgrade sometime in a distant future. I don't mind only executing R i386 version. Runtime and memory req. is very modest.
I think something like the following will do what you want:
#ECHO OFF
SETLOCAL ENABLEEXTENSIONS
SET RKEY=
SET RPATH=
FOR /F "tokens=* skip=2" %%L IN ('reg.exe QUERY HKLM\Software\R-core\R /f * /k ^| sort') DO (
IF NOT "%%~L"=="" SET "RKEY=%%~L"
)
IF NOT DEFINED RKEY (
ECHO Unable to qyery registry key HKLM\Software\Rcore\R
EXIT /B 1
)
FOR /F "tokens=2* skip=2" %%A IN ('REG QUERY %RKEY% /v "installPath"') DO (
IF NOT "%%~B"=="" SET "RPATH=%%~B"
)
IF NOT DEFINED RPATH (
ECHO Unable to query registry value %RKEY%\installPath
EXIT /B 2
)
IF NOT EXIST "%RPATH%" (
ECHO Found path for R (%RPATH%^) does not exist
EXIT /B 3
)
IF "%PROCESSOR_ARCHITECTURE%"=="AMD64" (
SET "PATH=%RPATH%\bin\x64;%PATH%"
) ELSE (
SET "PATH=%RPATH%\bin\i386;%PATH%"
)
Rscript myscript.r
First, we enable 'local' mode so all variables we set will revert when the batch file exits (even if you use 'CALL' to invoke it). Next, we unset the two variables used, so we can test whether they are set by later code.
The first for loop will execute once per result, so RKEY ends up set to the last key under \R, and sort will hopefully order them such that the newest installation will end up last. The inner if statement is just to make sure blank lines are ignored.
Next is a basic error check to ensure that rkey was set (in case the registry key doesn't exist, for ex).
The next for loop should only iterate once, and extracts just the value part from the installPath value in the selected key. The for is just used to skip irrelevant lines and tokens. Then a test whether the value was found, and whether the found value actually exists or not.
Finally, update the path based on the architecture, and run the script.
Thanks to #Extrarius, I corrected the code, such that it should run first time. I was rejected to do this as an edit.
#ECHO OFF
ECHO Searching for install path of latest version of R in registry...
SETLOCAL ENABLEEXTENSIONS REM This line will reset path when return
SET RKEY=
SET RPATH=
FOR /F "tokens=* skip=2" %%L IN ('reg.exe QUERY HKLM\Software\R-core\R /f * /k ^| sort') DO (
IF NOT "%%~L"=="" SET "RKEY=%%~L"
)
IF NOT DEFINED RKEY (
ECHO Unable to query registry key HKLM\Software\R-core\R
EXIT /B 1
)
FOR /F "tokens=2* skip=2" %%A IN ('REG QUERY %RKEY% /v "installPath"') DO (
IF NOT "%%~B"=="" SET "RPATH=%%~B"
)
IF NOT DEFINED RPATH (
ECHO Unable to query registry value %RKEY%\installPath
EXIT /B 2
)
IF NOT EXIST "%RPATH%" (
ECHO Found path for R (%RPATH%^) does not exist
EXIT /B 3
)
SET OLDPATH=%PATH%
IF "%PROCESSOR_ARCHITECTURE%"=="AMD64" (
SET PATH=%RPATH%\bin\x64;%OLDPATH%
ECHO Found %RPATH%\bin\x64
) ELSE (
SET PATH=%RPATH%\bin\i386;%OLDPATH%
ECHO Found %RPATH%\bin\i386
)
Rscript myscript.R

Error while running a .sh script via QProcess

I have written a QT GUI program where pressing a button will execute a .sh script. The contents of the script is-
echo -e 'attach database 'testdatabase.db' as 'aj';\n.separator ","\n.import ora_exported.csv qt_ora_exported' | sqlite3 testdatabase.db
basically the script will import a .csv to an sqlite database. And when the script file (script.sh) is run manually from linux terminal ($./script.sh) it successfully imports the .csv file into the database table.
But, when I call the script from my QT program
void MainWindow::on_importButton_clicked()
{
QProcess process;
process.startDetached("/bin/sh",QStringList()<<"/home/aj/script.sh");
}
it compiles successfully but gives an error message in console when the button is pressed at runtime.
Error: near line 1: near "-": syntax error
Error: cannot open "ora_exported.csv"
what could be causing this ???
EDITED
I have changed my .sh script now to--
echo -e 'attach database 'testdatabase.db' as 'aj';\n.separator ","\n.import /home/aj/ora_exported.csv qt_ora_exported' | sqlite3 testdatabase.db
Thus providing the path to my ora_exported.csv. As a result the runtime error [Error: cannot open "ora_exported.csv"] has gone but the other message [Error: near line 1: near "-": syntax error] is still coming.
Same as was observed in previous case, using ./script.sh is successfully importing data to sqlite3 db table file but QProcess is unable to.
echo is a built in command of a shell that may behave differently.
E.g. take this test script: echotest.sh
echo -e "123"
Now we can compare different results:
$ bash echotest.sh
123
$ zsh echotest.sh
123
$ dash echotest.sh
-e 123
You are probably on some Ubuntu-like OS, where /bin/sh redirects to dash. That would explain the error around "-". So if you are using echo, set you shell specificially or ensure that your script works on all common shells.
Additionally, you are messing up your quotations
echo -e 'attach database 'testdatabase.db' as 'aj';\n.separator ","\n.import /home/aj/ora_exported.csv qt_ora_exported'
results in (no quotations in the first line)
attach database testdatabase.db as aj;
.separator ","
.import /home/aj/ora_exported.csv qt_ora_exported
but you pobably want
echo -e "attach database 'testdatabase.db' as 'aj';\n.separator ','\n.import /home/aj/ora_exported.csv qt_ora_exported"
It looks strange that you are using external script to update database!
why you don't pass "ora_exported.csv" file name as a script argument? This would help solve the problem.
I was talking (typing) about this solution:
void MainWindow::on_importButton_clicked()
{
QProcess::startDetached("/bin/sh",
QStringList()<<"/home/aj/script.sh",
"<location of: 'ora_exported.csv' file>");
}

Couldn't canonicalise: No such file or directory

I am getting Couldn't canonicalise: No such file or directory error while getting single file using sftp.
here is what I am doing,
#!/bin/ksh
. /feeds/scripts/files.properties
filename=$1.txt
echo $filename
sftp $getusername#$getserver << EOF >> $logfile
cd /feeds/out/data/
lcd /feeds/files/
get $filename
bye
EOF
I am able to print/echo file name, but while executing scripts I am getting below error,
user:/feeds/scripts> ./fileReceiver.sh sample
sample.txt
Connecting to xxxxx.xxx.xxx...
Couldn't canonicalise: No such file or directory
Couldn't stat remote file: No such file or directory
File "/u/user/sample.txt" not found.
I don't know why it adds '/u/user' before file name. Can anyone please help?
Thanks in advance.
Solved!, I am sorry, my mistake. In property file, I had mentioned wrong server name. Server names looks very similar so couldn't figure it out. Anyways, thanks #devnull, I gone through it, its useful.
For me solution was removing / from directory name at end
Non-working
/folder-name/
Working
/folder-name

Resources