LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Can I execute a shell command and put the result in command field? (https://www.linuxquestions.org/questions/linux-general-1/can-i-execute-a-shell-command-and-put-the-result-in-command-field-894822/)

fran4tw 07-31-2011 11:18 PM

Can I execute a shell command and put the result in command field?
 
Sometimes I have long directory path to type in command field of linux shell, it is very easily mistyped.

Such as :

can I execute "pwd" in command line and make the "pwd" result shown in the command line area and let me modify the command line area to execute another command.

micxz 07-31-2011 11:35 PM

If the issue is having to type it often to get cd there I would make an alias in your home directory:
Code:

ln -s /long/path/to/some/where/good/
Now you have a nice symlink you can easily access by
Code:

cd good/
Alternatively you can name it something else:
Code:

ln -s /long/path/to/some/where/good/ fun
now you can cd fun/ hope this helps.

chrism01 08-01-2011 12:39 AM

@micxz: your first example is missing the link name
@fran4tw: http://linux.die.net/man/1/ln

catkin 08-01-2011 02:31 AM

I'm not sure what you are asking so this might not be relevant but ...
Code:

dir=$( pwd )
<now do whatever you want with $dir>


micxz 08-01-2011 03:39 AM

Quote:

Originally Posted by chrism01 (Post 4430369)
@micxz: your first example is missing the link name
@fran4tw: http://linux.die.net/man/1/ln

I know. Try it out. Check the "2nd form" in the man page you posted.

micxz 08-01-2011 03:44 AM

Right catkin or use $PWD
http://tldp.org/LDP/abs/html/internalvariables.html

catkin 08-01-2011 04:18 AM

Quote:

Originally Posted by micxz (Post 4430455)

Thanks micxz -- that's a lot better solution! :doh:

b0uncer 08-01-2011 06:30 AM

Quote:

Originally Posted by micxz (Post 4430455)

Those variables are often forgotten in daily use :)

Using the output of a program directly might be handy in other tasks than changing working directory, so in that sense I'd still vote for that approach. In addition it should guarantee that you get what you want to, even if the unthinkable happened and somehow $PWD got overwritten just before you typed the command (not probable, but possible).

There are other ways of using the output, or rather "other notations," than the $(command) one. For example in Bash one can also use backticks,
Code:

echo `pwd`
which may or may not be faster to type, depending on your keyboard and fingers. Check the manual of your shell if you're using something different than Bash (if that does not work).

onebuck 08-01-2011 08:40 AM

Hi,

Along with other suggestion if you have the need to repeat entries then why not setup a .bashrc & .bash_profile for your user with aliases;

Code:

sample .bash_profile;

 ~$ cat .bash_profile
 # .bash_profile
 #08-30-06 12:21
 #
 # Source .bashrc
 if [ -f ~/.bashrc ]; then
         . ~/.bashrc
 fi

Code:

sample .bashrc;
 :~$ cat .bashrc
 
 #.bashrc
 #08-30-06 12:20
 
 # Add bin to path
 
 export PATH="$PATH:/sbin:/usr/sbin:$HOME/bin"
 
#export PATH="$PATH:$HOME/bin"
 
# Dynamic resizing
 shopt -s checkwinsize
 
 # Custom prompt
 #PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
 
#08-29-06 11:40
 
 if [ `id -un` = root ]; then
   PS1='\[\033[1;31m\]\h:\w\$\[\033[0m\] '
  else
   PS1='\[\033[1;32m\]\h:\w\$\[\033[0m\] '
 fi
 #
 # Add color
 eval `dircolors -b`
 
 # User defined aliases
 alias cls='clear'
 alias clls='clear; ls'
 alias ll='ls -l'
 alias lsa='ls -A'
 alias lsg='ls | grep'
 alias lsp='ls -1 /var/log/packages/ > package-list'
 alias na='nano'
 alias web='links -g -download-dir ~/ www.google.com'
 
 #08-29-06 11:50

 #To clean up and cover your tracks once you log off
 #Depending on your version of BASH, you might have to use
 # the other form of this command
   trap "rm -f ~$LOGNAME/.bash_history" 0
 
#The older KSH-style form
 #  trap 0 rm -f ~$LOGNAME/.bash_history

The .bashrc is very useful!

Do not forget about 'history';
Quote:

excerpt 'man history';
NAME
history - GNU History Library

COPYRIGHT
The GNU History Library is Copyright (C) 1989-2002 by the Free Software Foundation,
Inc.

DESCRIPTION
Many programs read input from the user a line at a time. The GNU History library
is able to keep track of those lines, associate arbitrary data with each line, and
utilize information from previous lines in composing new ones.

HISTORY EXPANSION
The history library supports a history expansion feature that is identical to the
history expansion in bash. This section describes what syntax features are avail-
able.

History expansions introduce words from the history list into the input stream,
making it easy to repeat commands, insert the arguments to a previous command into
the current input line, or fix errors in previous commands quickly.

History expansion is usually performed immediately after a complete line is read.
It takes place in two parts. The first is to determine which line from the history
list to use during substitution. The second is to select portions of that line for
inclusion into the current one. The line selected from the history is the event,
and the portions of that line that are acted upon are words. Various modifiers are
available to manipulate the selected words. The line is broken into words in the
same fashion as bash does when reading input, so that several words that would oth-
erwise be separated are considered one word when surrounded by quotes (see the
description of history_tokenize() below). History expansions are introduced by the
appearance of the history expansion character, which is ! by default. Only back-
slash (\) and single quotes can quote the history expansion character.
Event Designators
An event designator is a reference to a command line entry in the history list.

! Start a history substitution, except when followed by a blank, newline, = or
(.
!n Refer to command line n.
!-n Refer to the current command line minus n.
!! Refer to the previous command. This is a synonym for `!-1'.
!string
Refer to the most recent command starting with string.
!?string[?]
Refer to the most recent command containing string. The trailing ? may be
omitted if string is followed immediately by a newline.
^string1^string2^
Quick substitution. Repeat the last command, replacing string1 with
string2. Equivalent to ``!!:s/string1/string2/'' (see Modifiers below).
!# The entire command line typed so far.
Just a few links to aid you to gaining some understanding;


1 Linux Documentation Project
2 Rute Tutorial & Exposition
3 Linux Command Guide
4 Bash Beginners Guide
5 Bash Reference Manual
6 Advanced Bash-Scripting Guide
7 Linux Newbie Admin Guide
8 LinuxSelfHelp
9 Utimate Linux Newbie Guide

The above links and others can be found at 'Slackware-Links'. More than just SlackwareŽ links!

fran4tw 08-08-2011 08:27 PM

Thanks to everyone. It really helps. But I am currently working as one man MIS, do'nt have enough time to test all these good suggestion shortly. All the suggestion will test and make the best use of them.

Best regards


All times are GMT -5. The time now is 10:42 AM.