Assign output of mkdir command to variable - shell

I am trying to assign the output of mkdir command to a variable. So I can use the directory further.
-bash-4.1$ pwd
/user/ravi/myscripts/tmpdata
-bash-4.1$ OUTPUT=$(mkdir tmpbkp.`date +%F`)
-bash-4.1$ ls | grep tmp
tmpbkp.2017-04-06
-bash-4.1$ echo "$OUTPUT"
But the directory name is not assigning to the variable. Could you please correct me where I am wrong.

When you run the mkdir command by itself, look how much output it produces:
$ mkdir foo
$
None!
When you use a command substitution to generate the argument to mkdir, look how much extra output you get:
$ mkdir tmpbkp.`date +%F`
$
None!
When you put it inside $() it still produces no output.
There is a -v option for mkdir (in the GNU version at least) which produces some output, but it's probably not what you want.
You want the name of the directory in a variable? Put it in a variable first, then call mkdir.
$ thedir=tmpbkp.`date +%F`
$ mkdir $thedir
$ echo $thedir
tmpbkp.2017-04-06
$

Related

Why the command in a file fails to work ? (FreeBSD 10.2)

https://www.youtube.com/watch?v=bu3_RzzEiVo
My intention is to experiment with shell scripts in a file. (FreeBSD 10.2)
I create a file named script.sh
cat > script.sh
set dir = `pwd`
echo The date today is `date`
echo The current directory is $dir
[Ctrl-d]
After giving it execution authority, I run the command
sh script.sh
I get
Why the directory is not displayed?
Then I make a change.
cat > script.sh
set dir = `pwd`
echo The date today is `date`
echo The current directory is `pwd`
[Ctrl-d]
This time , it works fine. The directory is shown successfully.
I would like to know why ? Could anyone tell me ?
The answer from TessellatingHeckler was on the right track.
From the man page for sh(1):
set [-/+abCEefIimnpTuVvx] [-/+o longname] [-c string] [-- arg ...]
The set command performs three different functions:
With no arguments, it lists the values of all shell variables.
If options are given, either in short form or using the long
``-/+o longname'' form, it sets or clears the specified options
as described in the section called Argument List Processing.
If you want a command for setting environment variables, that command would be setvar, which you'd use as follows:
setvar dir `pwd`
This is, however, uncommon usage. The more common synonym for this would be:
dir=`pwd`
or
dir=$(pwd)
Note that there are no spaces around the equals sign.
Note also that if you choose to use the setvar command, it's a good idea to put your value inside quotes. The following produces an error:
$ mkdir foo\ bar
$ cd foo\ bar
$ setvar dir `pwd`
Instead, you would need:
$ setvar dir "`pwd`"
Or more clearly:
$ dir="$(pwd)"
Note that you may also need to export your variables. The export command is used to mark a variable that should be passed along to sub shells that the running shell spawns. An example should make this more clear:
$ foo="bar"
$ sh -c 'echo $foo'
$ export foo
$ sh -c 'echo $foo'
bar
One other thing I'll add is that it's common and unnecessary to use date as you're doing in your script, since that command is able to produce its own formatted output. Try this:
$ date '+The date today is %+'
For date options, you can man date and man strftime.
Last tip: when using echo, put things in quotes. You'll produce less confusing and more reasonable output. Note:
$ foo="`printf 'a\nb\n'`"
$ echo $foo
a b
$ echo "$foo"
a
b
Hope this helps!

Bash: passing a variable to mv command option

--Bash 4.1.17 (running with Cygwin)
Hello, I am trying to pass the date into the --suffix option on the move (mv) command. I am able to pass in a simple string (like my name) but unable to pass in the date. If you run the script below you will see that the mv command with the suffix="$var" works but suffix="$now" does not.
#!/bin/bash
dir="your directory goes here"
now="$(date "+%m/%d/%y")"
var="_CARL!!!"
echo "$now"
echo "$var"
cd "$dir"
touch test.txt
# error if already exists
mkdir ./stack_question
touch ./stack_question/test.txt
mv -b --suffix="$var" test.txt ./stack_question/
The idea is that if test.txt already exists when trying to move the file, the file will have a suffix appended to it. So if you run this script with:
--suffix="$var"
you will see that the stack_question directory contains two files:
test.txt & test.txt_CARL!!!
But, if you run this script with:
--suffix="$now"
you will see that in the stack_question directory only contains:
test.txt
Any help on this would be greatly appreciated!
It is because you have embedded / in your date format try
now="$(date +%m_%d_%y)"

Shell subcommand output to parent command

I have a one line command, lets say
grep needle haystack.file
What if i wanted to replace "needle" with the current working directory using the pwd command. Now it might be possible using pipes but I need the needle part to show the working directory and the rest of the command to be the same.
So preferably something like this:
grep (pwd) haystack.file
Which when executed would actually run the following command:
grep /var/www/html/ haystack.file
I've done a bit of searching and have found a lot of examples with pipes but it cant be applied in my scenario as the first part (grep) and second part (haystack.file) is fixed in an application.
Use the $PWD variable, always set:
grep "$PWD" haystack.file
You can also use command substitution:
grep "$(pwd)" haystack.file
Note the importance of quotes. Do it! Otherwise strange things can happen.
You can use command substitution
Test
$ echo $(pwd) > test
$ grep $(pwd) test
/home/xxx/yyy
OR
$ grep `pwd` test
/home/xxx/yyy
Security
It's always recomended to quote the command substituion to take care of the spaces in the output of pwd command
Test
$ pwd
/home/xxx/yyy/hello world
$(pwd) > test
$ grep $(pwd) test #without quoting
grep: world: No such file or directory
test:/home/xxx/yyy/hello world
$ grep "$(pwd)" test #with quoting
/home/xxx/yyy/hello world

Alias with > into /dev/clipboard not working

In my cygwin's .bashrc I have the following two aliases:
alias dospath='cygpath -w `pwd`'
alias dospathcp='dospath > /dev/clipboard'
The first one is supposed to print the dos (or windows) path of the directory in which it is executed. This one works as expected.
The second alias is then supposed to redirect the output of dospath into /dev/clipboard so that I can paste it in windows applications. This one does not work. When I type dospathcp in bash, it just empties /dev/clipboard (and the clipbaord itself).
Try as follows:
alias dospath='cygpath -w $PWD'
alias dospathcp='dospath > /dev/clipboard'
This produces following output in my CYGWIN_NT-6.1-WOW64 CC 1.7.25(0.270/5/3) 2013-08-31 20:39 i686 Cygwin
$ alias dospath='cygpath -w $PWD'
$ cd /home/somedir
$ dospath
W:\cygwin\home\somedir
$ cd /home/anotherdir
$ dospath
W:\cygwin\home\anotherdir
$ alias dospathcp='dospath > /dev/clipboard'
$ cd /home/somedir
$ dospathcp
$ cat /dev/clipboard
W:\cygwin\home\somedir
$ cd /home/anotherdir
$ dospathcp
$ cat /dev/clipboard
W:\cygwin\home\anotherdir
See http://ss64.com/bash/alias.html
The first word of the replacement text is tested for aliases, but a
word that is identical to an alias being expanded is not expanded a
second time. This means that one may alias ls to "ls -F", for
instance, and Bash does not try to recursively expand the replacement
text.

How to get the value of the output of ls/dir command in a Linux shell console?

I need to to this operation automaticaly:
$ ls
this_folder
$ cp -rf this_folder/* .
For this I need to store in a variable the value of the "ls" so that I do something like this:
$ ls
this_folder
$ cp -rf $value_of_the_ls/* .
It is possible to do this? Give me some clues.
Best Regards,
To set the output of a command into a variable, use command substitution like this:
$ value_of_the_ls=$(ls)
$ echo "${value_of_the_ls}"
Have you thought about what you will do if there are multiple files in the current directory in which case ls will return multiple files?

Resources