error compressing netcdf files using nccopy through a loop - unix

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)

Related

echo to file - error: cannot create

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

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>");
}

unable to execute unix script using . script_name

I am unable to execute following command in unix as
e.g. ->
export ENVFILE=$PARENT_DIR/../env/.tpms.evr
while i try to execute . "${ENVFILE}" it shows me error as bash: 39910-: command not found
can anybody let me know about how to fix this.
You most probably have a line in .tpms.evr script which bash tries to execute as a command called "39910-"
Please share this .tpms.evr script or just that line containing "39910-"

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

Running date +%s command from a file

When I run the command date +%s directly on unix terminal, it correctly executes and gives number of seconds since 1970.
However, when the same command is in a script file, say temp.sh as below:
business_dt=date +%s
echo $business_dt
On executing above script, it throws error as below:
-ksh: +%s: not found [No such file or directory]
How to resolve this issue?
You need to use command substitution to assign the output of a command to a variable. The syntax is var=$(command). So, try changing your script to:
business_dt=$(date +%s)

Resources