ksh cannot cp from location with space in it? - shell

I am trying to do the following in ksh but keep getting cannot stat message for the cp command:
JMX_ROOT=/bfs-build/build-info/mep_mainline-Linux.latest/core/mainline/automation
SMOKE_JMX_LOCATION=$JMX_ROOT/"Smoke Set"/*.*
cp $SMOKE_JMX_LOCATION /var/tmp/tempor
Any ideas, have tried putting quotes around the various variables but with no luck. Think its something to do with the spaces in "Smoke Set" but don't how how to work it.
Many thanks.

JMX_ROOT=/bfs-build/build-info/mep_mainline-Linux.latest/core/mainline/automation
SMOKE_JMX_LOCATION="$(echo $JMX_ROOT/"Smoke Set"/*.*)"
cp "$SMOKE_JMX_LOCATION" /var/tmp/tempor

JMX_ROOT=/bfs-build/build-info/mep_mainline-Linux.latest/core/mainline/automation
SMOKE_JMX_LOCATION=$JMX_ROOT/"Smoke\ Set"/*.*
cp $SMOKE_JMX_LOCATION /var/tmp/tempor
Does this solve your problem? Adding a \ before the space.

try escaping the space with a backslash
SMOKE_JMX_LOCATION=$JMX_ROOT/Smoke\ Set/*.*

Related

How to remove everything from path after a word using sed in bash/ shell script?

Where the word is repeated and only wanted to be removed from specific word location
Lets say my path is - /opt/xyz/config/config.xml
Solution I want after using sed is: /opt/xyz/config/
how can this be obtained?
I am sick of using {sed 's/config.*//'} >> This actually removes both config words
such as it looks
/opt/xyz/
I have tried using this in multiple ways
> sed 's/config.*//'
Maybe with something like this?
sed 's/[^/]*$//'
But if the filepath is in a shell variable then you might as well use:
mydir=${myfilepath%/*}/
Another potential solution, depending on your use-case, is the dirname bash function, e.g.
dirname /opt/xyz/config/config.xml
/opt/xyz/config
Using BASH parameter expansion:
p="/opt/xyz/config/config.xml"; echo "${p%/*}"
/opt/xyz/config

shell scripting quotation

I have written a small script with which I take the name of a File.
#objectname
echo "objectname"
read ON
Can't get simpler.
I do some processing with the file I get.
gpg -c --no-use-agent "$ON"
For example if I have a file a.exe --> It will encrypt it and give me a file with a different md5 and an extension. Now, the file looks this way a.exe.gpg
Now, if I give it a bind the name of the file directly.
like this for example:
Taken from : this link
# This works
fileName='a.exe.gpg'
md5sum=$(md5sum ${fileName})
echo $md5sum
it returns it properly.
What if I want to do it dynamically.
This is what I tried:
#does not work
gpg -c --no-use-agent "$ON"
fileName= `$ON.gpg`
md5sum= $(md5sum ${fileName})
echo $md5sum
I get this bug here: upload.sh: 1: upload.sh: Fire.exe.gpg: not found and the program does not exit.
May I ask where exactly is the mistake I am doing?
The error is here:
fileName= `$ON.gpg`
There should be no space after =. (Also look at the next line.)
You used back-quotes, which execute $ON.gpg rather than simply evaluating it. Back-quotes are the same as $(...) but less elegant. Use double-quotes for this.
Read Greg's wiki entry on quotes for an ultra-detailed explanation with opinionated commentary. :-)
Be careful when making assignments in shell script. Don't use spaces in any sides of the operator=. Try the following:
fileName="$ON.gpg"
md5sum=$(md5sum ${fileName})
Note that the variable and the assignment operator= are together with no space.
Also, when you use backticks as `expression`, it will be executed by shell like using $(expression), as pointed by user ghoti.
You goofed on fixing the filename.
fileName="$ON.gpg"

Using environment variables to form paths in Make and windows

I have a make file and I am trying to use it to copy files to a directory. The path of the directory is stored in an environment variable. The problem is when I run make the C:\Data from the environment variable is interpreted as C:Data. How do I stop this being intrepreted as an escape character?
copyData : buildData
cp Release/*.tbl $(DATA)/index
results in:
cp Release/*.tbl C:\Data/index
cp: `C:Data/index': specified destination directory does not exist
Try `cp --help' for more information.
Actually, using forward slashes is the best, and correct, solution. Windows utilities always support forward slashes so it works, and trying to remember to always quote pathnames to avoid issues with backslashes is a major hassle.
In this case the first thing to note is that the problem is not a problem with make. make is passing the right content to the shell; it's the shell which is parsing the backslash as an escape character.
As I said above the right answer is to use forward slashes BUT if you want to allow people to use backslashes you'll have to go through your makefile and quote all arguments where a backslash might appear. For example:
copyData : buildData
cp Release/*.tbl '$(DATA)'/index
will fix your immediate problem.
If you have just a couple of these variables you could also do something like:
QDATA = '$(DATA)'
then remember to use $(QDATA) where you wanted the quoted value:
copyData : buildData
cp Release/*.tbl $(QDATA)/index
PS. Use forward slashes!! :-)

Problem with run commands in shell (bash) with arguments as variables !

(Sorry for the confusion. Previous $ sign occurred when I tried to simplify the actual problem. Thanks for correcting the question)
I wanted to split a directory name on underscores (ex: dir_to_split="my_test_dir") like this:
my_dir=($dir_to_split)
var=$(echo $my_dir | awk -F"_" '{print $1,$2,$3}')
set -- $var
splited_1=$1
splited_2=$2
splited_3=$3
now using these splited_x is causing me errors. ex.
myprograme $splited_1 $splited_2 $splited_3
Can anyone please help me with this ? Thank you....
(Rewritten after updated question.)
What kind of errors do you get? I find it useful to add set -x to the top of my shell scripts when debugging, this lets the shell print all commands it executes so you can pinpoint the line where problems begin.
Are you sure that $dir_to_split is actually set? Does it contain spaces or tabs? Does it contain two underscores? I don't see any other problems right now.
There are in-shell methods of splitting a variable such as:
dir="my_test_dir"
OIFS="$IFS"
IFS="-"
set --
IFS="$OIFS"
See also this SO question.

Run an EXE from a different directory?

After a bit of googling and searching here, couldn't find the answer to this silly question!
For a structure like this...
dirZero
|---dirOne
|---|---myProgram.exe
How do I run "myProgram" if my current directory is dirZero? I.E.,
C:\dirZero> dirOne/myProgram.exe
...which obviously doesn't work.
You should use a backslash \, instead of forward slash. /
C:\dirZero> dirOne\myProgram.exe
Or, wrap it with double quotes "
C:\dirZero> "dirOne/myProgram.exe"
Use a backslash instead
C:\dirZero> dirOne\myProgram.exe
probably u should just simple use
cd C:\dirZero\dirOne
C:\dirZero\dirOne> myProgram.exe

Resources