Simple wildcard use - variables

EDIT: I have just changed the if to an if else
IF "%CD:~1%"==":\" (
set FIR=%CD:~0,2%
) ELSE (
set FIR=%CD%
)
Trying to use a wildcard to detect if a batch file is on the root directory (As root causes there to be a backslash at the end of the DIR)
I need to determine with a simple if statement whether it is *:\ or not. I have tried using wildcards *:\ and ?:\ but to no avail. If I use the drive letter, example: G:\ it works.
if %CD%==G:\ echo SOMETHING WRONG HERE
works perfectly well, until the drive letter changes. Where as
if %CD%==?:\ echo SOMETHING WRONG HERE
or
if "%CD%"=="?:\" echo SOMETHING WRONG HERE
Doesn't work.
EDIT: After reading up it turns out if and wildcards are not compatible!

try this:
if "%CD:~-2%"==":\" (echo root) else echo NOT root
see docu help set.

Wildcards are not usable within IF statements in command.

Related

cronjob not working, but working in browser

So I have this weird behaviour:
A customers runs an oxid-shop. He bought a module and in its documentation, it stated:
Add 3 cronjobs: < url >
So, nothing too special so far. I tried it in the browser and everything worked fine. My output was:
Convert complete! 0 articles. File: google.xml
So the Script seems to work. Then I tried to combine it with a cronjob.
*/02 * * * * curl http://www.example.org/index.php?cl=param1&fnc=param2&rto=param3
as you can tell, a complete valid url (no special chars or something like this in the vars)
However, the output is the following:
Warning: Smarty error: unable to read resource: "" in /usr/www/users/.../www2/core/smarty/Smarty.class.php on line 1094
I also tried it with lynx -dump URL. Same output. I can't modify the script, since it is encrypted.
Any idea what I could try? Might the be due to the params?
Tried it on 2 different servers with the same outcome.
The problem could in deed be that there are parameters in your URL.
When using [] or {} sequences when invoked from a command line prompt,
you probably have to put the full URL within double quotes to avoid
the shell from interfering with it. This also goes for other
characters treated special, like for example '&', '?' and '*'.
Source: https://curl.haxx.se/docs/manpage.html
So you should try to enclose your URL with double quotes.

How can I use source script with variables in CygWin?

I'm trying to use external script with variables, but in result I get only "no such file or directory".
1st.ksh
#!bin/ksh
PATHNAME = `dirname $0`
. $PATHNAME/2nd.ksh
Echo $EXTVAR
2nd.ksh
#!bin/ksh
EXTVAR=1
I tried to use "Source" instead of "." (Source $PATHNAME/2nd.ksh) and I get the same result.
To run script I'm using full path to the script - cygdrive/e/Folder/1st.ksh.
2nd.ksh in this path too (cygdrive/e/Folder/).
All rights was granted for both files (chmod u=rwx,g=rwx,o=rwx filename).
If I put files in cygwin home path (/home/username/) I have the same.
Please help to understand what I'm doing wrong.
Thanks in advance!
$() should be used in ksh instead of `` (link)
. should be user instead of source (link)
"=" must not be surrounded with spaces. You should write: PATHNAME=$(dirname $0)
you should be aware of case-sensitiveness: echo, source

Batch Program Produces Strange Unexpected Output

Before I remembered how to accomplish what I was doing, I tried a couple different things, kind of just hacking at it.
What I was trying to accomplish was to set the following string as a variable and then echo it out in a batch script:
<?php require('__php__.php'); ?>
I eventually worked it out with help from SO, but before I got there, I tried this (for some reason):
set (phpStr=<? php require('__php__.php'); ?>)
Which I realize doesn't make any sense. However, how the cmd shell interpreted what I wanted to do was as follows:
set (phpStr= php require('__php__.php'); ? 0<? 1>)
In other words, when I typed the code in the second code block above, and turned on echo in the script, what showed up in the cmd shell was the command in the third code block. Then there was a syntax error, and the script exited.
Can anyone explain what happened? (Not why it didn't work. That is obvious to me, but rather, how it arrived at the interpretation it did. It's a pretty awesome restructuring of the original command. I just can't figure out how it got there.)
You need to escape redirection and other poison characters with ^ or the redirection will be active and try to create files etc. % is a special case.
You can also use something like this:
#echo off
for /f "delims=" %%a in ("<?php require('__php__.php'); ?>") do echo %%a

Checking for file existence

I have the following bat but it dosnt seem to work I want to check for a file name stoted in tom.txt, if it exists i want to do nothing, if however it dont exist i want to run the runme.bat
Echo Setting variable to file name
set FAT=<C:\tom.txt
ECHO Checking for file, if exists do nothing if not run bat...
if exists %FAT% (
end
)else(
C:\runme.bat
)
There are some minor mistakes, which, however, are the cause of your major difficulties.
You can read a line from a file with the SET /P command, not simply with SET:
SET /P FAT=<C:\tom.txt
The keyword in the file existence check command is EXIST, not EXISTS
IF EXIST …
Also, if you only need to react to the file's non-existence, you can simply add NOT:
IF NOT EXIST …
So, the entire command might be like this:
IF NOT EXIST %FAT% C:\runme.bat
I believe the correct syntax is
if exist %FAT% goto NORUN
C:\runme.bat
:NORUN
Notice "exist" vs "exists" in your code. A couple other things to note:
File NUL exists in any directory on any drive, therefore checking
for C:\NUL will always return true.
Checking for file existence
does not always work correctly on network devices.
See http://support.microsoft.com/kb/65994 for a bit more information.
I think the simplest solution would be to do it all on one line like this:
IF NOT EXIST C:\tom.txt C:\runme.bat
There is no need for the variable unless you intend on using it again, it just means another line of code. As Aleks G and Andriy M said, you need to make sure the commands and parameters are spelt correctly.

Space in path in a shell script

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"