04-27-2009
Assign bash command to variable
Hi
I am trying to write a function that needs to be able to assign the last run shell command to a variable. The actual command string itself not the exit code of the command.
I am using the bash command recall ability to do this as follows:
alias pb='ps | grep ash' #Last command string entered
VAR1="!!" #Method used to catch the last command string
Although this does work to capture the last run command, I have a feeling this is not very efficient.
Does anyone here have any suggestions or comments?
Thanks
10 More Discussions You Might Find Interesting
1. Shell Programming and Scripting
#!/bin/sh
#
##
MYSTRING = `awk '/myApp.app/' /Users/$USER/Library/Preferences/loginwindow.plist`
if
then
echo String not found
defaults write /Users/$USER/Library/Preferences/loginwindow AutoLaunchedApplicationDictionary -dict-add -string Hide -bool YES -string Path -string... (9 Replies)
Discussion started by: dedmakar
9 Replies
2. UNIX for Dummies Questions & Answers
Hi,
I am new to UNIX Scripting. I have been trying to use the CUT command to retrieve part of the header from a file and assign it to a variable. I have tried searching a lot, but I am still unsuccessful.
Sample Header: HJAN BALANCE 20090616
I need to retrieve the date here, which always... (10 Replies)
Discussion started by: ragz_82
10 Replies
3. Shell Programming and Scripting
greetings all,
I am have a heck of a time trying to accomplish a very simple thing. I have an array of "shortname<spaces>id" created from a dscl output. I want to assign shortname=word1 and id=word2. I have tried
shortname=$(${textArray} | awk '{print $1}') - and get 'awk : cannot open... (3 Replies)
Discussion started by: macnetdaemon
3 Replies
4. Shell Programming and Scripting
I'm running a bash script query and assigning the output to a variable like this:
exists=`mysql -u $USER_NAME --password=$PASSWORD -D "somedb" \
-e "SELECT * FROM somedb.sometable WHERE field1 ='$a' \
AND field2 ='$b' LIMIT 0 , 30";`
which returns something like:
echo... (2 Replies)
Discussion started by: unclecameron
2 Replies
5. Shell Programming and Scripting
Hi ,I am trying to assign string to variable ,but it doesn't work
Also could you show me different ways to use grep,(I am trying to get the first,second and first column form file,and I am counting the chars)
let name=`grep "$id" product | cut -c6-20` (25 Replies)
Discussion started by: lio123
25 Replies
6. Shell Programming and Scripting
Hi, wondering if it's been asked before but didn't find matches from google. Basically I have this line:
myvar=$(echo -e "a\tb")
Now somehow the '\t' from the echo output gets replaced with white space and then stored in $myvar.
It creates a problem for me later to use tab as delimiter to do... (2 Replies)
Discussion started by: birddie
2 Replies
7. UNIX for Beginners Questions & Answers
Hello All,
Hope you're doing well !
I am trying below command to be passed in a shell script, header_date_14 is a variable and $1 is the name of a file I intend to pass as a command line argument, however command line argument is not being accepted.
header_date_14=$(m_dump... (8 Replies)
Discussion started by: ektubbe
8 Replies
8. UNIX for Beginners Questions & Answers
In the else of the main if condition .
else
set lnk = $(readlink -f <path> | cut -d '/' -f7)
echo "$lnk"
if ]
When I run the above on command line , the execution seems to be fine and I get the desired output. But when I try to assign it to a variable within a loop... (12 Replies)
Discussion started by: sankasu
12 Replies
9. UNIX for Beginners Questions & Answers
Hi,
I have the script below. When i assign SSH_COMMAND to "ssh -o ConnectTimeout=2 ${SERVER} ${AS_SUDO} ${COMMANDS}" and then execute it as ${SSH_COMMAND} I get the following error:
ssh: Could not resolve hostname sudo: Name or service not known
ssh: Could not resolve hostname sudo: Name or... (3 Replies)
Discussion started by: mohca2020
3 Replies
10. UNIX for Beginners Questions & Answers
Hi,
I am attempting to assign the output of the following command, to two bash variables, var1 and var2 using "read," but it doesn't seem to be working.
# openstack hypervisor stats show | awk -F'|' 'NR==14{print $2,$3}'
vcpus 92
# echo $?
0
# openstack hypervisor... (4 Replies)
Discussion started by: sand1234
4 Replies