LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-03-2014, 08:04 AM   #1
massy
Member
 
Registered: Nov 2013
Distribution: CentOS 6.4
Posts: 209
Blog Entries: 1

Rep: Reputation: Disabled
","(shift+<) doesn't work as a IFS in my code!


I need to two first columns of uptime command so it's delimiter should be ","(shift+<) this code work correctly when IFS=":"!!!
Code:
utime=`uptime`
read d t rest<<<$(IFS=",";echo $utime)
echo $d $t
 
Old 03-03-2014, 08:10 AM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
First off,.. what is your original 'uptime' output. And also, what distro are you using?

Because it should work.
Code:
[root@dev ~]# read d t rest<<<$(export IFS=',';echo $utime)
[root@dev ~]# echo $d $t
09:05:43 up
as does this:
Code:
uptime|awk {'print $1 " " $2'}

Last edited by szboardstretcher; 03-03-2014 at 08:11 AM.
 
Old 03-03-2014, 10:04 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
hmmm ... I would suggest the awk is possibly missing something, like a change of delimiter

As for the command line, no need to over-think it:
Code:
$ IFS="," read d t _ <<<"$(uptime)"
$ echo $d $t
23:58:05 up 9 days 23:06
This is correct output as my uptime format is:
Code:
00:00:39 up 9 days, 23:09,  0 users,  load average: 0.00, 0.06, 0.52
So commas are not really helpful in mine, so hopefully will get better output with yours
 
2 members found this post helpful.
Old 03-03-2014, 11:03 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,871
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Try it in little steps:
Code:
read NOW UP UPTIME REST <<<$(uptime)
UPTIME=${UPTIME%,*}
printf 'NOW=%s UPTIME=%s\n' "$NOW" "$UPTIME"
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] "ifconfig eth0 up" doesn't work, cause of "no such file or directory" Sebi94 Linux - Networking 6 08-21-2012 10:39 PM
Bash: when an empty IFS does not work like a default IFS (info) catkin Programming 13 04-19-2012 09:40 AM
C++ - "snprintf" inside "for" doesn't work as expected. (int to char*) Repgahroll Programming 14 08-31-2010 08:27 AM
shift+End and Shift+Home key combo doesn't work right santana Linux - Hardware 6 04-23-2010 11:35 AM
Any way to get "Alice"; "Call of Duty" series and "Descent 3" to work? JBailey742 Linux - Games 13 06-23-2006 01:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:41 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