How to add sysdate to datafile name in shell scripting .
Ex : AAb.csv
Print like below
AAb7192017.csv
You can use this:
echo 'AAB`date +%m%d%Y`.csv'
If filename is stored in a variable then use,
echo ${VAR/./`date +%m%d%Y`.}
Related
I have a question about using the emacs modes within a shell script. It's often so, that I wrote just some pieces of code in PHP for example like this:
#!/bin/ksh
export FILENAME=$1
php -r '
$xml = new SimpleXmlIterator(getenv("FILENAME"), null, true);
echo "//" . $xml->attributes()["name"] . "\n";
echo "struct " . $sxi->attributes()["type"] . " {\n";
php blabla
'
cat FILENAME | while read x
do
.... shell blabla
Because of the shebang the emacs mode is forced to shell mode, which is correct.
But is there a trick to use the php mode "within" the part where php code is written?
(without to kick out the php in its own script)
Clearcase command line mkattr needs to wrap variable $bug_num between single quote + quote + $varible + quote + single quote, like this:
cleartool mkattr -replace BUGNUM '"$bug_num"' clearcase_file
How to make a call of the command cleartool mkattr in a Perl script in Unix env?
Env is Unix AIX and ksh
As mentioned in this recent answer:
If you want to execute a system command and don't have to use any shell syntax like redirects, it's usually better and safer to use the list form of system:
system(
'cleartool', 'mkattr', '-replace', 'BUGNUM ',
qq{"$bug_num"}, qq{clearcase_file}
);
# or, if you really want to pass both types of quotes:
system(
'cleartool', 'mkattr', '-replace', 'BUGNUM ',
qq{'"$bug_num"'}, qq{clearcase_file}
);
Or:
system(qq{cleartool mkattr -replace BUGNUM '"$bug_num"' clearcase_file});
I want to pass text in as a variable rather than a file.
I am not sure how to explain this well only with an example.
I have a perl script with usage as follows printme.pl [file]
I want to run without passing in a file and just passing text.
echo print_me | perl printme.pl
rather than doing
perl printme.pl textfile.txt
I am trying to run a perl script that takes a text file as a variable and outputs using echo.
bash (amongst other shells) has a feature called process substitution. In this feature, you can add some shell code that outputs to stdout and have the shell treat it like a file.
In your case, you would write:
perl printme.pl <(echo "hello world")
Perl will receive in $ARGV[0] a filename that looks something like /dev/fd/63 from which it can read the line "hello world\n"
In shell, I had sourced .cshrc file which contains some defined variables like user name.
I need to pass these variables to a certain Perl script.
For example in shell terminal, I typed
>echo $user
The output is >esaad
Then in Perl, to read $user variable, I tried:
system("echo $user")
Also tried this command:
my $userName = system( echo $ENV{user} );
but Perl asked for $user initialization as Perl variable not Shell one.
How I could read this variable?
You can:
print $ENV{'user'}
the reason your system call doesn't work is that system open a new shell that doesn't source .cshrc
read this answer for more information
Either use Perl built-in system variable $ENV:
print $ENV{'user'}
Or use backslash to escape the variable $user. Perl will interpret for $user variable defined inside the Perl program without the backslash. With backslash, "echo $user" is passed as system call.
system("echo \$user")
I have done a shell script that gets the output of a .jar file and assign in to a variable.
rewards_generator.sh
APP_ROOT=/home/testApps/
JAR=${APP_ROOT}ClusterGenerator/generator.jar
#get clusters
clusters=$(loadClusters $1)
for i in `echo $clusters | sed 's/,/ /g'`
do
#pull cluster records from database and save query return status to $x
x=$(/usr/IBM/WebSphere/ProcServer/java/bin/java ${JAR} ${i/-/_} 2>&1)
done
Basically, what I've done to the java app is to System.out.print the query return status. Then used the 2>&1 in bash in order to get into the output stream and assign the value to a shell script variable.
Now how can I get the return value of a perl script and assign it to a shell script variable? Is it the same as the one I've done above or is there any other approach to do this?
You can use backticks to record the output of an external command in a bash script.
Here's a simple example:
#!/bin/bash
# Execute the script, recording output to a variable
x=`/path/to/script.pl`
# Display or act on the output some time later
echo "script output: $x"
Now how can I get the return value of a perl script and assign it to a shell script variable?
#! /bin/bash
perl script.pl
return_value=$?