symlink to executable doesn't launch application, error: <symlink> doesn't exist - linux

I have a symlink to an executable, which I've created as follows:
$ ln -s /home/x/app/wps_office/wps
If on the commandline I type:
$ /home/x/app/wps_office/wps
Then my application launches correctly, but if I try to launch my application through the symlink, then I get the following error:
$ wps
wps does not exist!
Just to make sure if the symlink is correct;
$ readlink wps
/home/x/app/wps_office/wps
The folder /home/x/bin is where I've created the symlink, this folder is included in my $PATH variable.
I don't see what is going wrong here, why doesn't my application execute when I use the symlink?
Quick update;
I've just quickly looked trough the contents of the file where the symlink is pointing to, it looks like the message wps does not exist is actually coming from the application, meaning the symlink is actually correct. I don't know the exact reason why, as I find it strange that everything works correctly when I don't use the symlink. I need to look more thorougly to the code to find that out.
The code of the file where the symlink is pointing to:
#!/bin/bash
gOpt=
gTemplateExt=("wpt" "dot" "dotx")
gBinPath=$(dirname "$0")
if [ -d "${gBinPath}/office6" ]; then
gInstallPath=${gBinPath}
else
gInstallPath=/opt/kingsoft/wps-office
fi
gApp=wps
function parse_arg()
{
if [ $# -eq 1 ] ; then
ext="${1##*.}"
if [ "" = "${ext}" ] ; then
return 0
fi
for i in ${gTemplateExt}
do
if [ "${ext}" = "${i}" ] ; then
gOpt=-t
fi
done
fi
}
function run()
{
oldPwd="${PWD}"
if [ -e "${gInstallPath}/office6/${gApp}" ] ; then
if [ -d /usr/lib32/gtk-2.0 ]; then
export GTK_PATH=/usr/lib32/gtk-2.0
fi
${gInstallPath}/office6/${gApp} ${gOpt} "$#" || ${gBinPath}/wps_error_check.sh ${gInstallPath}/office6/${gApp}
else
echo "${gApp} does not exist!"
fi
}
function main()
{
parse_arg "$#"
run "$#"
}
main "$#"
Note the line where it says echo "${gApp} does not exist!", this is where my error is coming from.

Commands will only be executed without any path elements if they're part of the shell, or if they're in the PATH environment variable. Try
./wps
in the directory where the symlink is. Also confirm that the permissions are correct.

Change the line
gInstallPath=/opt/kingsoft/wps-office
in the script to
gInstallPath=/home/x/app/wps_office

The file where the symlink was pointing to, takes the current directory to launch a different file. This is the file actually being launched. The issue can be solved by simply creating a symlink to this file, which means a symlink to /home/x/app/wps_office/office6/wps
Another option is to edit the source file itself, as explained by #Pixelchemist. However as it concerns an application which I've downloaded and which I will probably update in the future, I think in this case that is not a preferred option.

Related

How can I copy files from one directory to another (which is a subdirectory in the original)?

I'm new to Linux shell script and I'm struggling with a problem. An error pops up telling that the if conditional has too many arguments. What I have to do is basically described on the title, but I've written a code that is not working, what's wrong with it? The original directory is called artists and the subdirectory where the files need to be copied to is called artists_copy.
#!/bin/bash
count=0
elem=$(ls)
for file in $elem; do
let count+=1
done
for i in {$count}; do
if [ -e $elem[$i] ]; then
cp $elem[$i] artists_copy
echo "Copied file $elem[$i] to artists_copy"
fi
done

Concatenating hardcoded directory and user-created text file adds root-level paths when it shouldn't

I have written a script to allow a restricted user access to deleting files on a production webserver. However, to prevent fat-fingering issues leading to accidental filesystem deletion/problems, I have hard coded the base directory in a variable... But the final result is not properly creating the desired path from hard-coded directory + user paths if they have a * wildcard...
I have an Apache 2.4.6 server that caches web content for a user. They have a jailkit user to SSH into this box. As this is production, they are severely limited in their access, however, I would like to give them the ability to clear specific cache directories on their own terms. In order to prevent this from going horribly wrong, I have hard-coded the base cache directory into a script variable, so that no matter what, the script will only run against that path.
So far, this script works well to iterate through their desired cache clear paths... A user creates a .txt file with a /cachePath defined on each line, and the script will iterate through it and delete those paths. It works just fine for /path and /content/path2/ ... But I cannot for the life of me get it working with wildcards (i.e. /path/, /content/path2/). There's probably a sexier way to handle this than what I've done so far (currently have an if | else statement for handling * or /* not included in the script below), but I am getting all kinds of undesired results trying to handle a user-inputted * or /* on a custom path.
#!/bin/bash
#For this to work, a user must create a paths.txt file in their jailed home directory, based off the /mnt/var/www/html cache location. Each location (or file) must be on a new line, and start with a /
#User-created file with custom cache directories to delete
file="/usr/jail/paths.txt"
#Setting this variable to the contents of the user-created cache file
pathToDelete=$(cat $file)
#Hard-coded cache directory to try to prevent deleting anything important outside cache directory
cacheDir="/mnt/var/www/html"
#Let's delete cache
if [ -f $file ];then
echo "Deleting the following cache directories:"
for paths in $pathToDelete
do
echo $cacheDir"$paths"
#rm command commented out until I get expected echo
output
#rm -rfv $cacheDir"$paths"
done
echo "Cache cleared successfully"
mv $file "$file.`date +"%m%d%Y%H%M"`"
else
echo "Nothing to do"
fi
I've tried double quotes, single quotes, no quotes, tried treating "pathToDelete" as an array, none of it is producing the desired output yet. For example, if paths.txt contains only "*", the result is grabbing all directories under / and adding them to "cacheDir"?
/mnt/var/www/html/testing/backup
/mnt/var/www/html/testing/bin
/mnt/var/www/html/testing/boot
/mnt/var/www/html/testing/data
/mnt/var/www/html/testing/dev
/mnt/var/www/html/testing/etc
/mnt/var/www/html/testing/home
/mnt/var/www/html/testing/lib
/mnt/var/www/html/testing/lib64
...
If paths.txt is "./*" it's adding files from the location of the script itself:
/mnt/var/www/html/testing./cacheClear.sh
/mnt/var/www/html/testing./paths.txt
Ultimately, what I'm looking for is this: if /mnt/var/www/html contains the following directories:
/content/
/content/path/
/content/path/file1.txt
/content/path/file2.txt
/content/path/subdir/
/path2/
/path2/fileA.txt
/path2/fileB.txt
Then a file containing
/content/path/*
should delete /content/path/file1.txt, file2.txt, and /subdir/, and preserve the /content/path/ directory.
If the paths.txt file contains
/content/path
/path2/*
Then /content/path directory and subfiles/directories should be deleted, and the files within /path2/ directory will as well... But right now, the script doesn't see the concatenated $cacheDir + $paths as a real / expected location if it contains a * anywhere in it. Works ok without * symbols.
Got a version that works well enough for my purposes:
#!/bin/bash
file="/usr/jail/paths.txt"
pathToDelete=$(cat $file)
cacheDir="/mnt/var/www/html"
if [ -f $file ]; then
if [ "$pathToDelete" == "*" ] || [ "$pathToDelete" == "/*" ]; then
echo "Full Clear"
rm -rfv /mnt/var/www/html/*
else
echo "Deleting the following cache directories:"
for i in ${pathToDelete};
do
echo ${cacheDir}${i}
rm -rfv ${cacheDir}${i}
done
echo "Cache cleared successfully"
fi
fi
The following code is a working solution:
#!/bin/bash -x
file="/usr/jail/paths.txt"
pathToDelete="$(sed 's/^\///' $file)"
cacheDir="/mnt/var/www/html"
if [ -f $file ];then
echo "Deleting the following cache directories:"
for paths in "$pathToDelete"
do
echo $cacheDir/$paths
rm -rfv $cacheDir/$paths
done
echo "Cache cleared successfully"
else
echo "Nothing to do"
fi

variable part in a variable path in ksh script

I'm sorry if something similar was already answered in the past, but I wasn't able to find it. I'm writing a script to perform some housekeeping tasks, and I get stuck in the step below. To put you in the record, it's a script which reads a config file in order to be able to use it as standard protocol in different environments.
The problem is with this code:
# Check if destination folder exist, if not create it.
if [ ! -d ${V_DestFolder} ]; then # Create folder
F_Log "${IF_ROOT} mkdir -p ${V_DestFolder}"
${IF_ROOT} mkdir -p ${V_DestFolder}
continue
fi
# If movement, check write permissions of destination folder.
V_CheckIfMovement=`echo $1|grep #`
if [ $? -eq 0 ]; then # File will be moved.
V_DestFolder=`echo $1|awk -F"#" {'print $2'}`
if [ ! -w ${V_DestFolder} ]; then # Destination folder IS NOT writable.
F_Log "Destination folder ${V_DestFolder} does not have WRITE permissions. Skipping."
continue
fi
fi
Basically I need to move (in this step) some files from one route to another.
It checks if the folder (name read from config file) exists, if not it will be created, after that check if the folder have write rights and move the files.
Here you can see the part of config file which is read in this step:
app/tom*/instances/*/logs|+9|/.*\.gz)$/|move#/app/archive/tom*/logs
I need to say the files are properly moved when I change the tom* of the destination for anything, as "test" or any word without * (as it should).
What I need to know is how I can use a variable in "tom*" in destination. Variable should contain the same name of tom* in the source, which I use as the name of the cell.
This is because I use different tomcat cells with the reference tom7 or tom8 plus 3 letters to describe each one. as example tom7dog or tom7cat.
You should give the shell a chance to evaluate.
V_DestFolder=`echo $1|awk -F"#" {'print $2'}`
for p in ${V_DestFolder}; do
if [ ! -w ${p} ]; then

Check $BINDIR value

First of all I need to apology, I have very little Unix/Linux knowledge.
I am following http://dns323.kood.org/howto:subversion to install SVN server on my DLink DNS323 device. I have successfully install Fonz Fun Plug. And I think I installed subversion-1.5.2-1.tgz as well.
According to the document I need to run svnserve.sh, in which part of the soruce code is
svnserve_start() {
if [ -x "{$BINDIR}/svnserve" ]; then
echo "Starting svnserve deamon... "
${BINDIR}/svnserve -d -r ${REPOSITORY}
else
echo "ERROR: svnserve not found or not executable"
fi
}
I got ERROR: svnserve not found or not executable message, so it looks like to me $BINDIR is not defined well.
Anybody knows, how can I do echo to display the value of $BINDIR?
Thanks!
echo $BINDIR before the if statement to check whether {$BINDIR}/svnserve exists.
It seems that {$BINDIR}/svnserve is not executable. Find that file and chmod u+x which add executable attr to it.

How do I create a directory in a makefile

I'm using Visual Studio 2005 nmake, and I have a test makefile like this:
sometarget:
-mkdir c:\testdir
I want to always create the directory, without having to specify 'sometarget'. For example, I can do this:
!if [if not exist c:\testdir\$(null) mkdir c:\testdir]
!endif
But that requires two lines, where I really only want to do the "-mkdir c:\testdir". If I just replace it with "-mkdir c:\testdir" I get an error from nmake - "fatal error U1034: syntax error : separator missing".
How can I always execute the mkdir, without messing about with !if [] stuff?
I think this will work:
-# if NOT EXIST "dir" mkdir "dir"
Make always wants to do things based on targets. It's not a general scripting tool. It looks at the targets and checks to see if they exist. If the target does not exist it executes the commands for that target.
The usual way to do this is to have a dummy target that is never going to be generated by the make scripts, so every time make runs it has to execute the relevant commands.
Or, you could add the command to a batch file that then calls your make file.
I'm not sure if there is an equivalent in Windows with nmake, but I managed to create a directory without using targets on Linux. I used the make function "shell". For example:
# Find where we are
TOPDIR := $(shell pwd)
# Define destination directory
ROOTFS := $(TOPDIR)/rootfs
# Make sure destination directory exists before invoking any tags
$(shell [ -d "$(ROOTFS)" ] || mkdir -p $(ROOTFS))
all:
#if [ -d "$(ROOTFS)" ]; then echo "Cool!"; else echo "Darn!"; fi
I hope Windows has the equivalent.
$(DIRNAME):
#[ -d $# ] || mkdir -p $#
Try using this:
-mkdir -p c:\testdir

Resources