Retrieve the last field in a string containing '/' as delimiter using tsch script - tcsh

I have writen a tcsh script. I have a file name and its path in a variable. For example : proj/debug/runs/filename
This path can be of any length. I need just the file name at the end. Is there any way to split this string and retrieve the file name?

If your path is stored in a shell variable, say $p, then the t modifier will do what you want:
% set filepath = /one/two/thee/four
% echo $filepath:t
four
alternatively, you can use the basename command.

Related

CMD to bash script conversion semicolumn

I am trying to convert a CMD script (.bat) into a .sh script on Linux side.
I did not find a proper documentation for instance for the following lines
set PATH="${PATH1}%;${PATH_NAME};"
another_script.bat -create "%LocalDestination%TEST;%LocalDestination%" -e %GenericEnvironementName% -d "%SettingsPath%/Env"
For the first one it is an export but I do not know it is like the if condition?
${PATH1}=${PATH_NAME}
export PATH=$PATH1
for the second one the expression
"%LocalDestination%TEST;%LocalDestination%" it's like an assignement? why we put the % at the end?
$LocalDestination$TEST = $LocalDestination
%GenericEnvironementName% will be $GenericEnvironementName
%SettingsPath%/Env >>> $SettingsPath/Env?
Variables in dos bat files are delimited with %, before AND after. So %VAR% is replaced by the value of VAR.
set PATH="${PATH1};${PATH_NAME};" assigns the values of PATH1 and PATH_NAME to variable PATH, separated by ;.
In Bash you would write: export PATH="$PATH1;$PATH_NAME"
Therefore, yes, any variable referencing is bash is done with $ before the variable name. So %TATA% becomes $TATA.
Example: %SettingsPath%/Env --> ${SettingsPath}/Env

checking for a file in ftp server through shell script

I've written a code in shell to retrieve the file of type "OLO2OLO_20170601_FATTURA.txt.zip" which is of current date.
Below is my code:
#!/bin/ksh
DATE=`date '+%Y%m%d'`
FILE="OLO2OLO_$DATE_FATTURA.txt.zip"
/usr/bin/ftp -n 93.179.136.9 << !EOF!
user abc 1234
cd "/0009/Codici Migrazione"
get $FILE
bye
!EOF!
But I'm getting below error:
$ ./ftp_test1
Failed to open file.
You have to put the variable name in curly brackets.
FILE="OLO2OLO_${DATE}_FATTURA.txt.zip"
An underscore is valid in a variable name. It is not a token separator.
Formally
name is a word consisting only of alphanumeric characters and
underscores, and beginning with an alphabetic character or an
underscore.
Currently shell is trying to substitute a value for a variable with name DATE_FATTURA which is empty so your FILE variable becomes OLO2OLO_.txt.zip Such file probably does not exist on the remote server.

bash : Matching a name with a template list in Unix

I am trying to match a file name with a list of templates stored in a variable .
My variable holding the templates to match is like below
TEMPLATE_LIST="Testing_????.csv TEST_??.csv Sample.???"
And my variable holding the file name is FILE_NAME.
If FILE_NAME="Sample.csv" or FILE_NAME="TEST_12.csv" like that command should return success . And if the FILE_NAME is like "TEST_123.csv" it should return Failure as it is not matching any template stored in the variable .
I was able to parse through the list and check file_name with one by one template in a loop , But that is a lengthy process .
How can we achieve this in a single command ?
You can just echo the TEMPLATE_LIST variable without quotes and let it be expanded by shell:
echo $TEMPLATE_LIST

Use standard input in a shellscript

We have to make a script that interacts with the standard input, wich is a file and we put a keystring on it, but the difficulty of this exercise is that the file can't be found in some cases, so we have to save the filename to a variable
key.sh (keystring) < (filename)
how can i save the filename into a variable?
In key.sh, you want to have a script like this:
#!/bin/sh
# assign the input string to the variable filename
filename="$1"
Then you would actually call the script with key.sh filename

batch file string character split

I want to write a batch file to store a character from a file name into a variable.
For example if my file name is helloworld or how_are_you?, I want to store fourth character from right into variable x (which in the above cases would be o and _).
File names do not have spaces in my case.
Assuming the file name is stored in a variable called filename, you can do this:
set "rstr=%filename:~-4%"
set "x=%rstr:~0,1%"
P.S
If you need filename to be passed as a command-line argument (for example, the first), use this before evaluating rstr and x:
set "filename=%1"

Resources