Space in path in a shell script - scripting

I have a variable
android="/media/New Volume_/android-sdk-linux_86"
I get an error saying that it cannot find the directory
I changed it to android="/media/New\ Volume_/android-sdk-linux_86"
I still get the same error. I even tried
android="/media/'New Volume_'/android-sdk-linux_86"
and I am using "$android" everywhere ...
I am not able to find the error, can someone help me..
Thanks

Take it this is bash? You assign variables with spaces like this:
android="/media/New Volume_/android-sdk-linux_86"
(Note you don't use the $ notation when setting a value.)
And reference them like this:
cd "$android"

Related

is there any way to splitting Variables in VSCode?

I wonder there is any way to splitting some variables in VSCode?
my example will explain my question better:
I have an exe file in such path C:\path\to\workspace\main\project\project.exe
my cpp source path that will create exe file is this C:\path\to\workspace\main\project\test.cpp
I want to create a task in tasks.json but my oder of variables does not give me the right path
as you understand:
${workspaceFolder} is C:\path\to\workspace
${fileDirname} returns C:\path\to\workspace\main\project
and ${relativeFileDirname}.exe returns main\project.exe
and combination of "${fileDirname}\\${relativeFileDirname}.exe" as a command will return C:\path\to\workspace\main\project\main\project.exe that is wrong.
so I wanted to know there is any other variable that just return the parent of current file or not?
if not can we split variables with \ ?
I hope it makes some sense
thanks
Add new fileDirnameBasename variable
see https://github.com/microsoft/vscode/commit/551db7ec94f02a4bdc8999092cf8bef642b3992d
${fileDirnameBasename} is being added to vscode v1.52 which I believe is what you are looking for.
You can use the extension Command Variable
Use the commands:
extension.commandvariable.file.fileDirBasename
extension.commandvariable.file.fileDirBasename1Up
extension.commandvariable.file.fileDirBasename2Up
btw, as an workaround, this worked for me
"${fileDirname}\\*.exe"
but need a variable for getting parent folder of current open file
any idea?

Pass parameter to SQL file from within another SQL file

I thought for sure there would be an SO question on this, but I haven't been able to find one.
I have 2 SQL files, myFile1.sql and myFile2.sql. myFile1.sql calls myFile2.sql like so:
-- In myFile1.sql:
#scripts/myFile2
This works with no problem, but now I'd like to pass an argument to the file. I've tried doing the following, with no success (results in a File Not Found exception):
#scripts/myFile2 'ImAnArgument'
Does anyone know what the syntax would be to do this?
I'm guessing your problem is that scripts/myFile2.sql is a relative path from the script it is located in. If that is so, then it is following that path from the directory where SQL*Plus was started (the current working directory). If this is the problem, then it's not the parameter that is the issue, but rather that SQL*Plus can't find the file. In this case, you should use ##, which invokes the path relative to the file it's located in.
The parameter should work just as you proposed (documentation). Parameters provided when invoking a file are placed into substitution variables (rather than bind variables) and can be referenced by using an ampersand followed by the argument number. In your example, 'ImAnArgument' would be &1.
After many attempts, I wasn't able to pass a parameter in (and I still don't understand why not). But here is what I did to get the same affect:
-- In myFile1.sql:
DEFINE my_arg = 'ImAnArgument';
#scripts/myFile2
Then
-- In myFile2.sql
-- Do stuff using the variable my_arg, such as
SELECT my_arg FROM my_table;

Defining variables with indirect expansion

I know that I can use indirect expansion to call variables whose names are contained in other variables as follows
VAR="test"
VARNAME="VAR"
echo ${!VARNAME}
However if I try to redefine a variable with this:
VARVALUE=0
VALUE="VARVALUE"
${!VARNAME}=${!VALUE}
echo ${!VARNAME}
It doesnt work, and I get
bash: test=0: command not found
I can see why this variable declaration fails, but I can't see how to fix it. In searching I've only found examples calling variables with indirect expansion, but not defining them so.
edit:
After a bit more searching, I've tried
eval "${!VARNAME}=${!VALUE}"
which throws
bash: =0: command not found
Fiddling with it some more, I've managed to find a solution
eval "$VARNAME=${!VALUE}"

Objective C Command Line Tool Argument Input

I have a bad problem and I hope you can give me some ideas! :)
What I want to do:
Simply read the user argument of the Script.
What is the problem:
special characters
Let me explain:
Scriptname: testscript
if the user types testscript -f filename.txt it prints out the name. Pretty easy.
But if the user types the following, it does not work for the shell: testscript -f file(somethings)name.txt
so if there are special characters, it always throws an sh: error....
Working: it works if the user writes this: testscript -f 'filename(something).txt'
with ' '
But this is inconvenient and often people forget that they have to write it.
Does anyone have an idea what I can do?
I thought about getting the argument and then add ' and ' at the beginning and end, I am not sure if the shell error is first..
Otherwise, does anyone have an idea!?
Would it be possible to check if there is an () somewhere in the filename before allowing userinput and remove () then?
Unfortunately, there's nothing your program can really do here - the error your users are seeing comes from the shell, before your program even has a chance to execute.

Makefile string variable

I need to create a variable with spaces in my Makefile:
CC=$(LFS_TGT)-gcc -B$(TOOLS)/release/lib/
What is a right way to do that?
UPDATE:
My code does not work, I have an error, something like bash cannot find command -B$(TOOLS)/release/lib/.
If I've guessed correctly as to what you mean, including extra quoting in the variable definition should work, i.e.:
TOOLS = 'dir with spaces'
CC = $(LFS_TGT)-gcc -B$(TOOLS)/release/lib/