Programming :: Store A Command In A Variable?

Jan 28, 2010

Code:

ls Again the command can be stored in a variable and then executed. Like

Code:

var=ls
&var

The above two codes are the same. The problem occurs when we try to pipeline it. Consider the following problem:

Code:

ls | grep *

works fine...but when we try to store it in a variable and run the command there is an error.

Code:

var="ls|grep *"
$var

how to store this kind of commands in a variable?

View 11 Replies


ADVERTISEMENT

Programming :: Receive Command Output And Store It To A Variable?

Apr 3, 2010

Suppose I want to account number of files beginning with abc , I can use "ls 'abc* | grep abc | wc -l", this will return me a number.
I want to store this number in a variable, say var1, so I tried
1. "ls 'abc* | grep abc | wc -l |read var1", but this didn't work as var1 has no value somehow.
2. var1='ls 'abc* | grep abc | wc -l', this just assign the entire string "ls 'abc* | grep abc | wc -l" to var1, which is not I wanted.

I don't want to store the value to a temporary file and then read the value from that file. I think there should be a direct way to get the value, but don't know how. I know in tcsh, one can just use set var1='ls 'abc* | grep abc | wc -l', but it also doesn't work in bash. Can anyone give any clue about this?

View 5 Replies View Related

Programming :: Using Variable To Store Command Lines In A Pipeline?

Jan 28, 2010

Was trying to write a shell script that has if conditional statements to decide different arguments for a command.Basically:

Code:
if [ "$1" = 1 ]
then

[code]...

View 6 Replies View Related

General :: Any Way To Store Output Of Command Into Variable?

Mar 3, 2010

I tried using the tail command in my shell script and storing that value in a variable a but an error keeps coming. Is there any other way to store the output of a command into a variable. Cannot Read text from text file and store it in a variable using shell script. The thing is I need a number from the file new.txt and use that number in my script

#!/bin/bash
a = `tail -1 new.txt|head -n 1`
echo $a

View 2 Replies View Related

General :: Store Result Of Wc -l Command In Variable?

Apr 19, 2011

I want to store the result of wc -l as a variable so I can use it later in my script...so far unsuccessfully.

I have tried this:

set `echo awk '{ print $1, $6}' | wc -l` | echo $1

but it is far from working.

View 11 Replies View Related

Programming :: Unable To Store Multiple Sed Operations Using Pipe In A Variable / Fix It?

Jun 2, 2011

I am not able to execute a multiple sed statement using pipe filters in a variable. i am trying to extract a path from a file and then working on that path to change a few letters within the path by going through another sed statement.

the code looks like this code...

View 2 Replies View Related

Programming :: Store Output Of Command With Pipes?

Jan 20, 2011

I need to find one RUNNING word in latest created logs.
and as soon as i will get RUNNING WORD , i have to execute another Unix Command.

I wrote following script code...

View 3 Replies View Related

Programming :: Calling Command From History Using Variable Like !$command?

Nov 19, 2008

suppose i store the history number of a command say :

1004 cat file

Then now i want to run it like : !1004 but by using a variable.

command=1004
!$command

i am getting errors like :

command=1004command

View 2 Replies View Related

Programming :: Using Variable In A Command?

Apr 14, 2011

i'm trying to execute a shell script, i'm trying to use the values in an array for use in a sed command:

sed -n '/Sales ID: ${array[$i]}/,/Totals:/p'

that command creates empty files. so my guess is that its not recognizing the array as an array but as text?
how would i be able to utilize the array in the command? i got it, didnt think that if i doubled up the single quotes that it would work, but this worked:

sed -n '/Sales ID: '${array[$i]'}/,/Totals:/p'

View 4 Replies View Related

Programming :: Change Variable In Case Command?

Aug 7, 2010

I want to display 4 options using the case command and refresh the screen when options 1 and 2 are chosen (no changes to the options and you get asked again to chose option), but give a message for option 3 and exit on option 4. I set this up with the script below, but choosing option 1 works and choosing option 2 exits the script.

Code:

#!/bin/bash
#testing options
Option="0"

[code]....

View 7 Replies View Related

Programming :: Pass A Shell Variable To An AWK Command?

Dec 23, 2010

I have the following code :

Code:
E_BADARGS=65
if [ $# -ne 2 ] ; then

[code]...

View 1 Replies View Related

Programming :: Assigning Bash Variable With The System Command In Awk?

Mar 1, 2011

I am having all sorts of trouble trying to assign a variable within an awk script with the system command. I know there is a lot of ways around this problem, but for efficiency reasons, I would like to, within my awk script, do something like

system(x=3)

or

system(x=NR)

and, latter on the shell script which calls the awk script, use the variable $x. But nothing is passed to x. I have already tried things like

command = "x=3"
system(command)

and also used a pipeline within the system to pipe it to /bin/sh In fact tried a lot of stuff like that, using $(( )) etc etc etc I can create directories e write to files (yes, i could write to a file and read from there, but I dont think it is efficient, plus I am puzzled).

View 7 Replies View Related

Programming :: Put A Variable In The $PS1 Prompt That Will Change Each Time A Command Is Run?

Feb 11, 2011

I would like to put a variable in the $PS1 prompt that will change each time a command is run. I want the color of the $PS1 prompt to change each time a command is run.I know that I can do this:

Code:
PS1="h@w # "
## "#" is changes every time a command is run

[code]....

View 9 Replies View Related

Programming :: Pass An Expect Command Result Into A Variable And Then Use It Again?

May 4, 2010

I'm trying to create a program that would locate the oldest file of a certain type on a server. Here's the commands:

OLDEST_PATH=`find -L / -depth -maxdepth 6 -mindepth 6 -type d | sort -f | head -1`
OLDEST_FILE=`find -L $OLDEST_PATH | grep .mp3 | sort -f | head -1`
ls -al $OLDEST_FILE

I'm writing this all in expect but I'm having problems. The main problem I have is whenever I try to run the first command, I can't seem to isolate the result of the OLDEST_PATH so that the 2nd command will work. There always seems to be a newline in the variable and the result is only "find -L" command running and it bypasses the variable. If I can just figure out how to get the 1st and 2nd command to work, then I can figure out the 3rd. Here's some code:

Code:
send "find -L / -depth -maxdepth 6 -mindepth 6 -type d | sort -f | head -1
"
sleep 20
expect -re "(.*)

[Code]....

I know there is a better way to write this. I've tried multiple ways and this just happens to be the last way I've tried it. If you try running this, you'll notice that there is still carriage returns after the result of OLDEST_PATH and it prevents the 2nd "find" command from working properly.

View 2 Replies View Related

Programming :: Variable Substitution In Sqlite Open Db Command?

Jun 15, 2010

I have the following TCL code:

if{ [catch {sqlite3 db /path/to/db/file} result] } {
puts stderr $result
} else {
do something
}

What I want is to use a variable for the file name/path. When I put in a variable instead of the absolute path, I get an error: "missing close-brace: possible unbalanced brace in comment while executing"

This is weird because when I run the code with the fully qualified pathname, it works fine. I substitute out the pathname for a variable containing the pathname ($variable) and I get this error.

View 1 Replies View Related

General :: Need To Store Output Of Bitset() In A Variable / Possible In C++?

Mar 5, 2010

I need to store the output of bitset() in a variable ... is it possible in c++?

View 1 Replies View Related

Programming :: Bash Script: Assign Command Ouput To A Variable Without Executing It?

Feb 12, 2011

I'm trying to execute the following command within a bash script:

Code:
tac /var/log/system.log | head -1
The script I wrote is:

[code]...

View 1 Replies View Related

Ubuntu Servers :: Bash Variable Multiple Lines - Store In $a?

Apr 30, 2010

I have this script:

Code:
a=awk'{print $8}' f1.txt

How can I make it store lines in $a ? In the way above it creates one big line.

View 2 Replies View Related

Programming :: Bash - Read Content Of File To Variable And Use This Variable In For Loop ?

Aug 21, 2009

I'm trying to read content of file to variable and use this variable in for loop. The problem is, when I have c++ comment style in file - /*. Spaces in line are also interpreted as separated lines.

For example:

Code:

Changing $files to "$files" eliminate these problems but causes that whole content of variable is treated as one string (one execution of loop).

View 6 Replies View Related

Programming :: Make A New Variable With The String From The Old Variable Btut Without Any Plus Sign?

Apr 7, 2010

my script has a variable which comes in the form +00.00 +0.00 -00.00 or -0.00 (the numbers can be any in that form) for any that have a + symbol I need to remove the +, but if it has a - symbol it needs to stay.

i need to make a new variable with the string from the old variable btut without any plus sign. I have tried a lot of different ways with no success, each thing I tried either left the + or removed the entire string. I think this should work but doesn't

foo=+12.40
bar=${foo#+}

View 4 Replies View Related

Programming :: Search Within A Variable And Assign The Results To A New Variable?

Apr 25, 2011

how I can search within a variable and assign the results to a new variable. I'll use the following as an example -

cars="Audi BMW Cadillac Chevy Dodge Ferrari Ford Mercedes"
list=`echo ${cars} | egrep -o '<A?+|<C+'`

with the echo command I get the following output assigned to list -

A
C
C

What I'd like to get for output is -

Audi
Cadillac
Chevy

how I could do this regardless of upper/lower case letters?

View 5 Replies View Related

Programming :: Storing Output Of "time" Command To A Variable

Jan 21, 2011

In my script, I need to get execution time of a command (say 'ls') in mili seconds level. For this i tried using "time" command to retrieve the total execution time in milli seconds. But, the problem is that, how to save the output of time command in a variable. The format of the command is like "time ls -R /opt" Going further, the o/p of 'time' command is:

real 0m0.003s
user 0m0.004s
sys 0m0.000s

Here, in my script, I would like to use only middle line "user 0m0.004s" saved to the variable but unable to find out the way.

View 1 Replies View Related

Programming :: Assign Value Of C Variable To Shell Variable?

Apr 28, 2010

included shell script inside c program, and i wanted to assign the value of c variable to shell variable..Can any one please suggest me how to do it?

View 8 Replies View Related

Programming :: Assignment To A Variable Variable?

Mar 17, 2011

This loop is part of a bash script which takes multiple arguments.

Code:
for ((i=1;i<=$number;++i)) ; do
offset=$(($i+5))

[code]...

View 3 Replies View Related

General :: Store The Output Of Date And Watch Command To A File

Jun 15, 2011

I am trying to watch a command and try to log it into a file. I tried

watch -t -n 10 "(date '+TIME:%H:%M:%S'
; ps aux | grep "pattern" | wc -l)" >>
logfile

and am expecting a result like

TIME: 10:32:30 12
TIME: 10:32:40 18
TIME: 10:32:50 2

to be stored in logfile. However, when the logfile has unprintable characters in in. How do I get this kind of output from the command li

View 4 Replies View Related

General :: Where Does ITunes Store Its Stuff - Copy Its Data Store To An External Drive

Dec 8, 2009

I have two students whose windows laptops are riddled with malware and not working properly. They want me to help them install Linux (which we use in school), but they are concerned about their iTunes.

Having avoided iPods as "defective by design", I know nothing about iTunes whatsoever. However I remember reading about DRM locking and such problems that have me concerned that I won't be able to do it.

Where does iTunes store its stuff?

Can I copy its data store to an external drive, and then into a linux home?

Then will it work on wine, or can another manager (rhythmbox etc) access the itunes data?

Alternatively, if I partition the drive and install linux, can rythmbox/wine/something access itunes data on the win partition?

Supposing they are buying music through iTunes, what will happen to that account?

Finally, one of them has an iphone. Does that work with linux?

Ironic that an apple application is blocking migration away from windows.

View 9 Replies View Related

Programming :: Store The Results In An Array?

Apr 13, 2010

i have a loop which produces new value everytime it runs. i want to store the results in an array. how can i do dat??

loop is like:
a=0
declare v
while ((....))
do
v[$a]="$r"
let a=a+1
done

while doing this it is not treating v as array but as variable and it is overriding the value and old value is lost..i am using bash shell version 3.00.15

View 5 Replies View Related

Programming :: Store H.264 File On Avi Container?

Mar 4, 2010

i m trying to store h.264 in avi container using ffmpeg....is it possible using ffmpeg.i m using v4l to capture a image and compressed in to h.264, how i can store it in avi container,any programming example,header format for avi container,and what other information required to store in a container.

View 14 Replies View Related

Programming :: Store A Packet In Buffer?

May 19, 2010

writing a code in C program for debugging error and sendingror messagein routingprotocols. please answer with code

View 2 Replies View Related

Programming :: Store The Data In Program In A Map Of Priority_queues?

Jul 30, 2011

I am trying to store the data in my program in a map of priority_queues an I am having a bit of trouble doing it. basically I am storing activities (ie watching tv, eating dinner, playing ping-pong, etc) and I want these event grouped by the day that they happened on.
my first thought was to use a multimap where the key is the date, and the value is the activity, however I would like to keep the activities in a specific order. I have tried to find the proper syntax for this but have frustratingly come up empty. Even google provided little assistance.

View 9 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved