How to get error message from ditto command , when it fails to archive - objective-c

Using ditto command we are archiving folder. When folder contains some files which does not have read permission. It fails to archive. That time ditto command logs error message saying " ditto: "Path" : Permission denied. How to get this error message.

As with any UNIX command, errors are written to stderr, which can be captured by adding 2> file to end of the command:
$ ditto src dst 2> error
$ cat error
ditto: /Users/andy/tmp/src/./x: Permission denied
If you are running ditto from a shell script, then something like this should work:
#!/bin/sh
errfile=/tmp/errors.$$
(cd ~/tmp; ditto src dst 2> $errfile)
if [ $? -ne 0 ]; then
echo There was a problem:
cat $errfile
else
echo Everything is cool
fi

Related

Is it possible to use the "code" command in SSH'ed terminal to open VS Code on local machine with SSH extension?

Something I love about VS Code is that when I am using a terminal in WSL, I can run code file.txt, and it will open that file with VS Code on my local using the WSL remote extension.
Is it possible to do a similar thing with SSH? I.e., if I am SSH'ed into a remote host, is it possible to set things up so that running code file.txt will open VS Code on my local machine, connected via the remote SSH extension to open that file?
I found much better & simple answer thanks to this post.
Simply create new script file named code with below contents & put file under any folder from $PATH. (echo $PATH to see what folders you can use)
#! /usr/bin/env zsh
local max_retry=10
for i in {1..$max_retry}
do
local script=$(echo ~/.vscode-server/bin/*/bin/remote-cli/code(*oc[$i]N))
if [[ -z ${script} ]]
then
echo "VSCode remote script not found"
exit 1
fi
local socket=$(echo /run/user/$UID/vscode-ipc-*.sock(=oc[$i]N))
if [[ -z ${socket} ]]
then
echo "VSCode IPC socket not found"
exit 1
fi
export VSCODE_IPC_HOOK_CLI=${socket}
${script} $# > /dev/null 2>&1
if [ "$?" -eq "0" ]; then
exit 0
fi
done
echo "Failed to find valid VS Code window"
Bash version
#! /bin/bash
max_retry=10
for i in $(seq 1 $max_retry)
do
recent_folder=$(ls ~/.vscode-server/bin/ -t | head -n$i)
script=$(echo ~/.vscode-server/bin/$recent_folder/bin/remote-cli/code)
if [[ -z ${script} ]]
then
echo "VSCode remote script not found"
exit 1
fi
socket=$(ls /run/user/$UID/vscode-ipc-* -t | head -n$i)
if [[ -z ${socket} ]]
then
echo "VSCode IPC socket not found"
exit 1
fi
export VSCODE_IPC_HOOK_CLI=${socket}
${script} $#
if [ "$?" -eq "0" ]; then
exit 0
fi
done
echo "Failed to find valid VS Code window"
Update
Above script doesn't work with recent updates. I had to change first line to
local script=$(echo ~/.vscode-server/bin/*/bin/remote-cli/code(*oc[1]N))
Update2
Original script may fail if recently opened ssh window is closed, yet there is another SSHed window open. I have enhanced the script to enable retrying the command with recent N(default 10) windows.
You shouldn't have to do anything. VSCode automatically sets the path/PATH to the code in the path/PATH environment variable depending on your shell. See this response. You might be overwriting your path/PATH like I was. I was accidentally overwriting path in ~/.cshrc and PATH in ~/.bashrc and was running into the same issue. After fixing it, I can run code on the command line. which code returns the location of the command.
Until I spent time to figure it out, I was using the two methods mentioned below. Both of which worked for me in bash; you can modify it for your shell as you see fit. But really fix your path/PATH rather than using these methods.
Adding location of code to the PATH in ~/.bashrc
export PATH=${VSCODE_GIT_ASKPASS_NODE%/*}/bin:$PATH
OR
Setting alias to code in ~/.bashrc
alias code="${VSCODE_GIT_ASKPASS_NODE%/*}/bin/code"
More on path vs. PATH here and here
Yes, sort of.
From a VSCode terminal run the command
env | grep VSCODE_IPC_HOOK_CLI
then copy-and-paste that line that line with export into your ssh terminal.
After that, you should be able to run code from your ~/.vscode-server/bin/XXX/bin directory.
VSCode terminal
SSH terminal
Update:
You can to automate this with a .bashrc and .profile to place the IPC code into a temp file, and source that when you do your ssh login.
For example, this works for me...
Append this to ~/.bashrc
#
if [ "$VSCODE_IPC_HOOK_CLI" != "" ]; then
cat >$HOME/.vscode_env.sh <<EOF
#
if [ "\$VSCODE_IPC_HOOK_CLI" = "" ]; then
export VSCODE_IPC_HOOK_CLI="$VSCODE_IPC_HOOK_CLI"
alias code="${VSCODE_GIT_ASKPASS_NODE%/*}/bin/code"
fi
EOF
fi
And append this to your ~/.profile
[ -f $HOME/.vscode_env.sh ] && . $HOME/.vscode_env.sh
(There may be more elegant ways. And you still have to start at least 1 terminal in your remote VSCode session.)
this works to me
if [[ -n "$SSH_CLIENT" || -n "$SSH_TTY" ]]; then
local script=$(echo ~/.vscode-server/bin/*/bin/remote-cli/code(*oc[1]N))
if [[ -z ${script} ]]
then
echo "VSCode remote script not found"
exit 1
fi
local socket=$(echo /run/user/$UID/vscode-ipc-*.sock(=oc[1]N))
if [[ -z ${socket} ]]
then
echo "VSCode IPC socket not found"
exit 1
fi
export VSCODE_IPC_HOOK_CLI=${socket}
alias code=${script}
fi
Use the below commands to open a folder or a file on the remote terminal.
Note: vscode-server must be already installed on the remote host (It would be, if you have already connected to it). Also the absolute path has to be specified for the file or folder. Use -n to launch in new window,-r to reuse same window.
code --folder-uri <absolute-path>
code --file-uri <absolute-path-file-name>
Example:
code -r --folder-uri /home/myscripts/src
code -n --file-uri /home/myscripts/src/math/sample.py

Executing BTEQ file via shell script (BTEQ: Command not found error)

I'm trying to set up an environment to execute BTEQ script via shell script in the local machine. On running the shell script I'm getting an error of BTEQ: Command not found. Not sure what I'm doing wrong.
I've created a separate .tdlogon file which contains .LOGON credentials. BTEQ script is a simple create table statement that I'm trying to execute.
My .tdlogon file is something like
.logon servername/uname,pwd
I'm calling the file like this
#!/bin/bash
server_path=/Users/xyz/xyz
log_path=/Users/xyz/xyz/logs
echo -e 'Starting the script'>> ${log_path}/test_log.log
cat ${server_path}/.tdlogon ${server_path}/code/temp_query.btq | bteq >> ${log_path}/test_log.log 2>&1
if [ ${rtn_code} -ne 0 ] ; then
echo -e 'Script completed successfully'>> ${log_path}/test_log.log
exit 0
else
echo -e 'Error in the script'>> ${log_path}/test_log.log
exit 1
fi
On executing the above code I'm getting below error in the log file
line 10: bteq: command not found
Appreciate any guidance related to this.
Seems like your Linux is not pointing to the bteq path. Update the bteq path:
export PATH=/usr/bin/bteq:$PATH
And, in some cases, there will be bteq32 instead of bteq in that case set path as:
export PATH=/usr/bin/bteq32:$PATH

appledoc- runscript- Expected end of line but found identifier

I'm trying to run this script as part of generating documentation
using appledoc plug in. But, I'm receiving this 'Expected end of line
but found identifier.' error when I run this script in Xcode(or
terminal). Please help me out as I'm new to scripting and i'm unable to identify the exact error in the code!
#!/bin/bash
tmpdir="tmpdocs"
finaldir="docs"
rm -rf $tmpdir
mkdir $tmpdir # we killed this last time, create it to avoid warnings from appledoc
cd ./AppledocTemplates/
git checkout master
git pull origin master
expectedversion=$(./templateversion.sh) # Check repo version
cd ..
##########################################
# Install templates if not current
##########################################
if [ ! -f ~/.appledoc/templateversion.sh ] #If this file doesn't exist, then need to install templates
then
cd ./AppledocTemplates/
./installtemplates.sh
if [ $? -ne 0 ] # Descriptive error message is sent in installtemplates. Just exit with error
then
exit 1
fi
cd ..
fi
version=$(~/.appledoc/templateversion.sh) # Check installed version
if [ $version -ne $expectedversion ]
then
echo "Updating templates"
cd ./AppledocTemplates/
./installtemplates.sh #wrong version - try installing
version=$(~/.appledoc/templateversion.sh)
if [ $? -ne 0 ] # Descriptive error message is sent in installtemplates. Just exit with error
then
cd ..
exit 1
fi
if [ $version -ne $expectedversion ] # Now is the version correct? If not, exit with error
then
cd ..
echo "You do not have the correct version of the appledoc templates"
echo "Make sure you run installtemplates.sh to put them in their correct location."
exit 1
fi
cd ..
fi
##########################################
# Compile the docs
##########################################
appledoc ./AppledocSettings.plist MySDK # Compile the docs
if [ $? -ne 0 ]
then
echo "Compile failure of source documents. MySDK doc creation not completed."
exit 1
fi
##########################################
# Stage docs in proper places and cleanup
##########################################
#Move the docs to final directory
rm -rf $finaldir # clean out whatever was in the final dir
mkdir $finaldir # and recreate it
#Copy the docset file to the docs directory so that it can be loaded into github
cp -a ~/Library/Developer/Shared/Documentation/DocSets/us.MySDK.MySDK-Total-SDK.docset ./$finaldir
if [ $? -ne 0 ]
then
echo "Unable to copy docset to ./docs"
exit 1
fi
# stage the html directories to their final destination
mv -f ./$tmpdir/html/* $finaldir
if [ $? -ne 0 ]
then
echo "Unable to move html files to ./docs"
exit 1
fi
rm -rf $tmpdir #clear out the tmp dir
echo "MySDK doc creation successful."
exit 0
The first one is the log screen shot if I run the script from the terminal.
Second is the log screen shot if I run the script from Xcode.

Handle gsutil ls and rm command errors if no files present

I am running the following command to remove files from a gcs bucket prior to loading new files there.
gsutil -m rm gs://mybucket/subbucket/*
If there are no files in the bucket, it throws the "CommandException: One or more URLs matched no objects".
I would like for it to delete the files if exists without throwing the error.
There is same error with gsutil ls gs://mybucket/subbucket/*
How can I rewrite this without having to handle the exception explicitly? Or, how to best handle these exceptions in batch script?
Try this:
gsutil -m rm gs://mybucket/foo/* 2> /dev/null || true
Or:
gsutil -m ls gs://mybucket/foo/* 2> /dev/null || true
This has the effect of suppressing stderr (it's directed to /dev/null), and returning a success error code even on failure.
You might not want to ignore all errors as it might indicate something different that file not found. With the following script you'll ignore only the 'One or more URLs matched not objects' but will inform you of a different error. And if there is no error it will just delete the file:
gsutil -m rm gs://mybucket/subbucket/* 2> temp
if [ $? == 1 ]; then
grep 'One or more URLs matched no objects' temp
if [ $? == 0 ]; then
echo "no such file"
else
echo temp
fi
fi
rm temp
This will pipe stderr to a temp file and will check the message to decide whether to ignore it or show it.
And it also works for single file deletions. I hope it helps.
Refs:
How to grep standard error stream
Bash Reference Manual - Redirections
You may like rsync to sync files and folders to a bucket. I used this for clearing a folder in a bucket and replacing it with new files from my build script.
gsutil rsync -d newdata gs://mybucket/data - replaces data folder with newdata

Handling DCL ON ERROR actions after first error?

The OpenVMS DCL command HELP ON EXAMPLE displays:
ON
Examples
1.$ ON SEVERE_ERROR THEN CONTINUE
A command procedure that contains this statement continues
to execute normally when a warning or error occurs during
execution. When a severe error occurs, the ON statement signals
the procedure to execute the next statement anyway. Once
the statement has been executed as a result of the severe
error condition, the default action (ON ERROR THEN EXIT) is
reinstated.
According to the help if neither [-]x.for nor [-]y.for exist then the last two lines will not be executed:
$ on error then $ continue
$ rename [-]x.for []
$ rename [-]y.for []
$ type *.for
Is there a way to set the ON ERROR handling as in the first line w/o placing an ON ERROR statement between each line of the script?
If the ON ERROR fires, you have to re-establish it. It looks like you
don't know whether any of the files exists. So the ON ERROR needs to be
re-established after the first failing command.
You can do this in a subroutine, like in:
$ on error then $ gosub on_error
$ rename [-]x.for []
$ rename [-]y.for []
$ on error then $ exit
$ type *.for
$ exit
$
$ on_error:
$ on error then $ gosub on_error
$ return
Also, you can handle this differently, with disabling error checking (SET
NOON):
$ set noon
$ rename [-]x.for []
$ rename [-]y.for []
$ set on
$ type *.for
or establishing error handling only for sever errors (ON SEVERE_ERROR):
$ on severe_error then $ exit
$ rename [-]x.for []
$ rename [-]y.for []
$ on error then $ exit
$ type *.for