ls output into a read command as a variable


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users ls output into a read command as a variable
# 1  
Old 12-14-2011
ls output into a read command as a variable

I'm working on a short BASH script on my Ubuntu box that will run powerpoint scripts with MS Powerpoint Viewer 2007 via WINE.

I can run the presentation when I run it manually but what i'd like to do is have the script look for the newest file then run it.


Code:
#! /bin/sh

# Start the newest Powerpoint presentation

cd /powerpoint

ls | sort -n -t _ -k 2 | tail -1 > /tmp/newest.txt

cat /tmp/newest.txt | while read newppt

wine "C:\Program Files\Microsoft Office\Office12\PPTVIEW.EXE" /N /powerpoint/$newppt


Any ideas? I think im almost there but im having trouble turning the output of the ls command into a variable
# 2  
Old 12-14-2011
ls has a 'sort by date' option, and you can pipe directly into read without the need for a temp file (although you don't actually need read in this case).

Try
Code:
ls -tr | tail -1 | read newppt

or
Code:
newppt=$(ls -tr | tail -1)

# 3  
Old 12-14-2011
The latter is more bash friendly, as you need an explicit sub-shell, not always convenient, as '|(read newppt ; ... $newppt ....)' as otherwise the bash 'read' after '|' is in an implicit sub-shell like '|(read newppt), and the $newppt value is inaccessible to everything.

I usually do less i/o, as there is no point in typing more to make the thing you want the last! As fork is cheaper than exec, and no exec's cheaper than any exec's,if this fits your needs, it is fastest at 1 fork:
Code:
ls -t | (
 read newppt
 . . . $newppt . . . .
 )

or (2 forks and an exec):
Code:
newppt=$( ls -t | line )

or if line is not installed:
Code:
newppt=$( ls -t | head -1 )

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read several variables from command output via SSH

Hi Folks, I'm currently trying to read several values into different variables. Actually, what I'm doing works, but I get an error message. My attempts are: read strCPROC strIPROC strAPROC <<<$(ssh -n -T hscroot@$HMC "lshwres -r proc -m $strIDENT --level sys -F \"configurable_sys_proc_units... (11 Replies)
Discussion started by: NKaede
11 Replies

2. Shell Programming and Scripting

How to read the output of a command line by line and pass it as a variable?

Hi, I have some 2000 names in a table like below. Java Oracle/SQL ANSI SQL SQL,DWH,DB DB&Java And by using for loop in my code i am able to get a single word but if there is any special character or space then it is considering as a next line. I have to execute the below queries in... (10 Replies)
Discussion started by: Samah
10 Replies

3. Shell Programming and Scripting

Output read variable

Hi all, how read varaible and ouput in colum, e.g. $ echo $VAR1 opc op odi games gopher vcsa abrt I like $ echo $VAR1 opc op odi games gopher vcsa abrt (3 Replies)
Discussion started by: aav1307
3 Replies

4. Shell Programming and Scripting

How to read the variable from awk output?

I am reading an xml file with date tag as <Date>Default</Date> using the below command. Dt=$(awk -F'' '/<Date>/{print $3}' /home/test/try.xml and getting the value from the xml file stored in this variable "Dt" echo $Dt gives me a value. Dt=Default. Now according to my requirement, If... (2 Replies)
Discussion started by: Saidul
2 Replies

5. Solaris

How to read the output of snoop command?

Hi! I have run the following command: snoop -q -d e1000g0 -o /var/tmp/optima0.txt & them I am trying to read the output of it with snoop -i /var/tmp/optima0.txt, which is giving me this: # snoop -i /var/tmp/optima0.txt | more 1 0.00000 AIOPTSVR -> 10.100.4.72 TCP D=1393 S=22 Push... (8 Replies)
Discussion started by: fretagi
8 Replies

6. Windows & DOS: Issues & Discussions

Read command result as variable

Hi all, Is there a simple way to read a command output as a variable? I've running a batch file to do: attrib.exe * | find /c /v "" >filecount.txt but rather than write it to the txt file I'd like to read the total in as a variable to pass to the rest of the bat file... Any help much... (1 Reply)
Discussion started by: Grueben
1 Replies

7. Shell Programming and Scripting

read line and run a different command according to the output

Hi. I'm trying to write a script that reads a line on a file and runs a different command for a different line output. For example, if it finds the word "Kuku" on the line it sends mail to Kuku@kuku.com. Otherwise, it sends mail to Lulu@lulu.com. TIA. (2 Replies)
Discussion started by: Doojek9
2 Replies

8. UNIX for Dummies Questions & Answers

read command - using output from command substitution

Hey, guys! Trying to research this is such a pain since the read command itself is a common word. Try searching "unix OR linux read command examples" or using the command substitution keyword. :eek: So, I wanted to use a command statement similar to the following. This is kinda taken... (2 Replies)
Discussion started by: ProGrammar
2 Replies

9. Programming

How to read output of a shell command

Hello All, I have a an application written in C and runing on Red Hat Linux. In my code I have written a command that is fired on the linux shell by using system() function call. Now I need to read the output of this command in my c program and assign it to a variable. Can anyone... (1 Reply)
Discussion started by: shamik
1 Replies

10. Shell Programming and Scripting

Using 'defaults read' and storing the output in a variable

Hi all, I'm creating a script which uses 'defaults read' to retrieve details from an Info.plist like this; defaults read "/Path/Contents/Info" CFBundleShortVersionString This works fine in Terminal and returns the expected values. Is it possible to use this command in a script, and... (0 Replies)
Discussion started by: davewg
0 Replies
Login or Register to Ask a Question