LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 08-01-2011, 12:18 AM   #1
fran4tw
LQ Newbie
 
Registered: Sep 2007
Posts: 2

Rep: Reputation: 0
Talking 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.
 
Old 08-01-2011, 12:35 AM   #2
micxz
Senior Member
 
Registered: Oct 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Rep: Reputation: 75
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.
 
1 members found this post helpful.
Old 08-01-2011, 01:39 AM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

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

Last edited by chrism01; 08-01-2011 at 01:40 AM.
 
1 members found this post helpful.
Old 08-01-2011, 03:31 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
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>
 
Old 08-01-2011, 04:39 AM   #5
micxz
Senior Member
 
Registered: Oct 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Rep: Reputation: 75
Quote:
Originally Posted by chrism01 View Post
@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.
 
Old 08-01-2011, 04:44 AM   #6
micxz
Senior Member
 
Registered: Oct 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

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

Last edited by micxz; 08-01-2011 at 04:45 AM.
 
1 members found this post helpful.
Old 08-01-2011, 05:18 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
Quote:
Originally Posted by micxz View Post
Thanks micxz -- that's a lot better solution!
 
Old 08-01-2011, 07:30 AM   #8
b0uncer
Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
Quote:
Originally Posted by micxz View Post
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).
 
1 members found this post helpful.
Old 08-01-2011, 09:40 AM   #9
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 11,478
Blog Entries: 4

Rep: Reputation: 1526Reputation: 1526Reputation: 1526Reputation: 1526Reputation: 1526Reputation: 1526Reputation: 1526Reputation: 1526Reputation: 1526Reputation: 1526Reputation: 1526
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!
 
1 members found this post helpful.
Old 08-08-2011, 09:27 PM   #10
fran4tw
LQ Newbie
 
Registered: Sep 2007
Posts: 2

Original Poster
Rep: Reputation: 0
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
execute shell command inside of c code? khucinx Programming 6 10-26-2010 10:30 PM
how to execute Source command in a shell script UltraSoul Linux - General 3 09-23-2010 11:13 AM
PHP can't execute shell command ! rome Programming 3 11-15-2005 01:46 PM
PHP can't execute shell command ! rome Programming 1 11-15-2005 10:12 AM
cannot execute 'cd /' shell command from PL/SQL.... samyakm Linux - Newbie 1 01-03-2005 11:10 AM


All times are GMT -5. The time now is 08:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration