LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-28-2020, 08:59 PM   #1
LYC
Member
 
Registered: Jan 2014
Posts: 109

Rep: Reputation: Disabled
Script queries


Hi,

When I execute the following in command line, it works

$ for i in {1..930}; do date >> output.log; prstat -a 1 1 >> output.log; sleep 60; done

When I put them in a file as follows and run the file, it only run once:

#!/usr/bin/sh

for i in {1..930}
do
date >> output.log
prstat -a 1 1 >> output.log
sleep 60
done

Seeking for advice?
 
Old 03-28-2020, 10:26 PM   #2
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,789

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
The { } expansion is bash - not standard shell.
Code:
#!/bin/bash

for i in {1..930}
do 
  date
  prstat -a 1 1
  sleep 60
done >> output.log
Also it is more efficient to redirect the whole loop, so the output file is opened once when the loop starts (and closed when the loop ends).
 
2 members found this post helpful.
Old 03-28-2020, 10:58 PM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,305
Blog Entries: 3

Rep: Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720
POSIX shell is guaranteed portable and faster. But Bash has more features and is the default on most GNU/Linux systems and older OS X.

So if you need portability to run this on a BSD, avoid functions unqiue to bash:

Code:
#!/bin/sh

for i in $(seq 1 930)
do 
  date
  prstat -a 1 1
  sleep 60
done >> output.log
Edit: Oops. That is POSIX but not working on OpenBSD because of the lack of seq which is part of the coreutils package. Here's an actually portable version.

Code:
#!/bin/sh

i=0
while test $i -lt 930
do 
  date
  prstat -a 1 1
  i=$(($i+1))
  sleep 60
done >> output.log

Last edited by Turbocapitalist; 03-29-2020 at 03:31 AM.
 
1 members found this post helpful.
Old 03-29-2020, 03:30 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
offtopic, but I think $(seq 1 930) is definitely slower than {1..930}.
I would prefer your second solution.
 
Old 03-29-2020, 03:33 AM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,305
Blog Entries: 3

Rep: Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720
Quote:
Originally Posted by pan64 View Post
offtopic, but I think $(seq 1 930) is definitely slower than {1..930}.
Quite right. The part about POSIX being faster should be constrained to mean when external programs are avoided.
 
Old 03-29-2020, 04:39 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,789

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
The POSIX compliant
Code:
i=0
while test $i -lt 930
do
  i=$((i+1))
  echo $i
done
echo $i
behaves like
Code:
for i in {1..930}
or
Code:
for i in $(seq 1 930)
To complete this discourse, I want to also mention the posixly correct
Code:
i=0
while test $((i+=1)) -le 930
do
  echo $i
done
echo $i
that behaves like
Code:
for ((i=1; i<=930; i++))

Last edited by MadeInGermany; 03-29-2020 at 04:43 AM.
 
  


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] Run bash script w/ sql queries using cron (RHEL 5.7, Oracle 11g) gacanepa Linux - Newbie 2 10-12-2013 09:49 AM
Script to mail output of Mysql Queries adarshmca Linux - Newbie 2 03-15-2011 07:58 PM
Script to mail Mysql Queries adarshmca Linux - Newbie 1 02-15-2011 07:48 AM
PHP Script to retrieve queries from log file saravanan1979 Programming 1 03-17-2002 08:13 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:52 AM.

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