LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 10-31-2008, 07:24 AM   #1
ZAMO
Member
 
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 598

Rep: Reputation: 30
help needed in scripting


hostname give the output as
server2.mydomain.com

I need to execute 2 commands in the same line

Code:
hostname ; ls -lt /home/zamo|grep -v total |head -1
It gives me an output like this ;
Code:
server2.mydomain.com
drwxrwxr-x   2 zamo zamo       4096 Oct 31 12:40 myscripts
Inorder to bring the output into a single line I modified the command to

Code:
 echo -ne `hostname` \\t ; ls -lt /home/zamo |grep -v total|head -1
Again inorder to cut the server2.mydomain.com to server2

Code:
echo -e `hostname` |awk -F\. '{print $1F}'  ; ls -lt /home/zamo |grep -v total|head -1
the output is
Code:
server2
drwxrwxr-x   2 zamo zamo       4096 Oct 31 12:40 myscripts
To make the output to a single line, i tried with
Code:
echo -e `hostname` |awk -F\. '{print $1F}'  \\t; ls -lt /home/zamo |grep -v total|head -1
echo -e `hostname` |cut -d '.' -f1,1  \\t; ls -lt /home/zamo |grep -v total|head -1
Both failed to get the desired output


I have an issue in using the \\t after awk -F\. '{print $1F}' and |cut -d '.' -f1,1.

Is there any other option to get my output as


Code:
 server2 drwxrwxr-x   2 zamo zamo       4096 Oct 31 12:40 myscripts
 
Old 10-31-2008, 07:55 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Can't you just use variables, e.g.

hostname=`hostname | awk -F '.' '{print $1}'`
ls_output=`ls -lt /home/zamo|grep -v total |head -1`
echo "$hostname $ls_output"
 
Old 10-31-2008, 08:23 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
A correct form of your command (using awk) can be
Code:
echo -e `hostname` |awk -F\. '{printf "%s\t",$1}'  \\t; ls -lt /home/zamo |grep -v total|head -1
where you use printf to avoid the newline character resulting from the print statement. Another simple way (similar to the one suggested by Nylex) is
Code:
echo -e "$(hostname -s)" "\t" "$(ls -lt | grep -v total | head -1)"
or eventually
Code:
printf "%s\t%s\n" "$(hostname -s)" "$(ls -lt | grep -v total | head -1)"
the -s option of hostname gives the short name, without the domain.
 
  


Reply



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
Scripting help needed. ZAMO Linux - General 1 09-17-2008 10:35 PM
Scripting help needed. ZAMO Linux - General 2 09-09-2008 06:11 AM
a little scripting help needed!! vatsan1 Linux - General 6 09-02-2007 08:50 AM
Scripting Help needed - IP Enumeration AnonyMooseUK Programming 11 10-22-2006 10:17 AM
Scripting help needed. stonelee Linux - Software 2 09-29-2003 09:47 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:43 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
Open Source Consulting | Domain Registration