Shell command date - shell

I want to print date in the mm-dd-yy format in shell script. From shell terminal I can get it using the following command:
date +"%d-%m-%y"
But I want it in the shell script and in a variable which could then be appended to a file name. I tried the following:
#!/bin/sh
mydate=`"date +\"%m-%d-%Y\""'
echo "$mydate"
But it is giving an error date +"%d-%m-%y" is not found.
Can anybody point out what mistake am I making?

You don't need quotes
mydate=`date +%m-%d-%Y`
will work.

Use
mydate=$(date "+%m-%d-%Y")
See this is a way to store a command in a variable: var=$(command). To use date, you define the format like date "+%format%place%holders", with + inside the double quotes.
$ mydate=$(date "+%m-%d-%Y")
$ echo $mydate
09-29-2014
Note it is preferred to use $() over ``, because it allows nesting multiple commands.

You have advice about how to do it properly. The reason for the error is the first level of inner double quotes makes the entire command with arguments into a single word:
mydate=`"date +\"%m-%d-%Y\""'
You are trying to execute a command named:
date +"%m-%d-%Y"
and clearly no such command exists.

#!/bin/sh
mydate=`date +\"%m-%d-%Y\"`
mydate1=`date +%m-%d-%Y`
echo "$mydate"
echo "$mydate1"
Both approaches will work. But the first one will have the date value surrounded by double quotes, something like "09-29-2014".

Related

bash replace variable name in string with variable value

This is kind of a weird one. I have the following string:
I have a variable called REDIRECT set to: https://working.${MYDOMAIN}/blah/blah.
I need to replace the ${MYDOMAIN} with the actual value of the variable assigned to ${MYDOMAIN}.
Not sure if bash or sed is best for this. I tried bash replace but couldn't get it to work, probably related to escaping the characters or something. Any help appreciated!
You may use this bash substitution:
echo "${REDIRECT/\${MYDOMAIN\}/$MYDOMAIN}"
or else, if you have envsubst utility then use:
export MYDOMAIN
envsubst <<< "$REDIRECT"
Just execute in bash:
eval REDIRECT=$REDIRECT

How to follow _ after $ in bash?

I have a shell script that need to use a variable following a dash to generate the file names.Here is what I did:
var="5"
echo "filename$var_something.txt"
However, it always output error, because the script treats $var_something as a variable.
How can I solve this?
Thanks.
echo "filename${var}_something.txt" use braces to delimit your variable name.

passing command line arguments to a shell script doesn't work

I want to write a script that will change to different directories depending on my input. something like this:
test.sh:
#!/bin/bash
ssh machine001 '(chdir ~/dev$1; pwd)'
But as I run ./test.sh 2 it still goes to ~/dev. It seems that my argument gets ignored. Am I doing anything very stupid here?
Bash ignores any variable syntax inside the single-quoted(') strings. You need double quotes(") in order to make a substitution:
#!/bin/bash
ssh machine001 "(chdir ~/dev$1; pwd)"
The parameter is enclosed in single quotes, so it isn't expanded on the local side. Use double-quotes instead.
#!/bin/bash
ssh machine001 "chdir ~/dev$1; pwd"
There's no need for the (...), since you are only running the pair of commands then exiting.

How do I assign the results of the rename command to a variable?

i am trying to set the output of a command to a variable in a script. i have searched all day and it looks like i have the command right. but each time i run my script, it just runs the command instead of only assigning it to a variable and not running it.
here is the relevant part of the script:
ffname=$(rename 's/_1080p_12000//' *.*)
echo $ffname
i need to use ffname as a variable in another part of the script without running the command.. but it doesnt seem to wanna work. anyone have any suggestions?
You can add a -n(no-act) option to rename to get the output.
ffnames=($(rename -n 's/_1080p_12000//' *.* | awk '{print $1,$NF}'))
The result contains multiple items. So you need a array to hold them.
Then do real renaming without -n option.
If you're using Bash, you can use the shell parameter expansion substitution operator on a populated variable to replace the portion of the filename that you want to delete. For example:
$ ffname=foo_1080p_12000.bar
$ echo "${ffname/_1080p_12000}"
foo.bar
However, it will remain up to you to perform the actual mv or rename commands, or to re-assign the modified value back to ffname if you like.

Problem in running a script

i have unix shell script which is need to be run like below
test_sh XYZ=KLMN
the content of the script is
#!/bin/ksh
echo $XYZ
for using the value of XYZ i have do set -k before i run the script.
is there a way where i can do this without doint set -k before running the script. or is there something that i can do in the script where i can use value of the parameter given while running the script in the below way
test_sh XYZ=KLMN
i am using ksh.
Any help is appreciated.
How about running this?
XYZ=KLMN ./test_sh //running from directory where test_sh is
If your script needs no other arguments, a quick and dirty way do to it is to put
eval "$#"
at the start of your script. This will evaluate the command line arguments as shell commands. If those commands are to assign a shell/environment variable, then that's what it will do.
It's quick-and-dirty since anything could be put on the command line, causing problems from a syntax error to a bad security hole (if the script is trusted).
I'm not sure if "$#" means the same in ksh as it does in bash - using just $* (without quotes) would work too, but is even dirtier.
It looks like you are trying to use the environment variable "INSTANCE" in your script.
For that, the environment variable must be set in advance of executing your script. Using the "set" command sets exportable environment variables. Incidentally, my version of ksh dates from 1993 and the "-k" option was obsolete back then.
To set an environment variable so that it is exported into spawned shells, simply use the "export" command like so:
export INSTANCE='whatever you want to put here'
If you want to use a positional parameter for your script -- that is have the "KLMN" value accessed within your script, and assuming it is the first parameter, then you do the following in your script:
#!/bin/ksh
echo $1
You can also assign the positional parameter to a local variable for later use in your script like so:
#!/bin/ksh
param_one=$1
echo $param_one
You can call this with:
test_sh KLMN
Note that the spacing in the assignment is important -- do not use spaces.
I am tring this option
#!/bin/ksh
echo $1
awk '{FS="=";print $2}' $1
and on the command line
test_sh INSTANCE=LSN_MUM
but awk is failing.is there any problem over here?
Probably #!/bin/ksh -k will work (untested).

Resources