Re: [SLUG] bash question
hi all, after reading up on ssh config options, i've gone with some Host sections in ~/.ssh/config: Host glebe1 User johndoe Hostname xxx.xxx.xxx.xxx does what i want without any worries of 'doing it the csh way' ;-) cheers justin -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
[SLUG] bash question
hi all, i have a bunch of aliases in ~/.bash_aliases like: alias glebe1='ssh [EMAIL PROTECTED]' alias glebe2='ssh [EMAIL PROTECTED]' if i just run any of the aliases from the command line, all is well. if i try to run: for BOX in glebe1 glebe2 ; do $BOX some command done bash throws: bash: glebe1: command not found bash: glebe2: command not found what am i missing about the way bash evaluates aliases? cheers justin -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
Re: [SLUG] bash question
- Bash evaluates aliases before variable expansion. You want shell functions instead. In fact, aliases are for people used to the csh way of doing things .. I'd *never* use them Try: function glebe1() { ssh [EMAIL PROTECTED] $@ } function glebe2() { ssh [EMAIL PROTECTED] $@ } -- Dr Peter Chubb http://www.gelato.unsw.edu.au peterc AT gelato.unsw.edu.au http://www.ertos.nicta.com.au ERTOS within National ICT Australia -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
Re: [SLUG] bash question
On 3/6/07, Peter Chubb [EMAIL PROTECTED] wrote: - Bash evaluates aliases before variable expansion. ah, that's what i'm missing. You want shell functions instead. ok, i'll try that. In fact, aliases are for people used to the csh way of doing things .. I'd *never* use them never used csh before - i'm just a programmer masquerading (badly) as a sysadmin... thanks for the pointers. cheers justin -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
Re: [SLUG] bash question
justin randell wrote: In fact, aliases are for people used to the csh way of doing things .. I'd *never* use them never used csh before - i'm just a programmer masquerading (badly) as a sysadmin... If thats the case you'd probably be better off using Python or Perl. As a programming language, shell has some major weirdnesses. Erik -- +---+ Erik de Castro Lopo +---+ Who would have believed that reading and writing would pay off? -- Homer Simpson -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
Re: [SLUG] bash question
On 06/03/07, Erik de Castro Lopo [EMAIL PROTECTED] wrote: justin randell wrote: In fact, aliases are for people used to the csh way of doing things .. I'd *never* use them never used csh before - i'm just a programmer masquerading (badly) as a sysadmin... If thats the case you'd probably be better off using Python or Perl. As a programming language, shell has some major weirdnesses. It's also more portable and has fewer dependencies. For simple tasks such as a simple for loop like this, the overhead of loading the interpreter is.. well, questionable. That said, there are definitely better alternatives that could be considered: I'd be looking at dsh (http://www.netfort.gr.jp/~dancer/software/dsh.html.en) for a start. -- There is nothing more worthy of contempt than a man who quotes himself - Zhasper, 2004 -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
Re: [SLUG] bash question - how to tell if apache has stopped?
Hi all Thanks Oscar. I didnt know about the pidof command. With your example of using the return value I now have my script working. I tested it out by killing apache with -9 and leaving a PID file around. The test for the PID file like [ -f $APACHEPID ] would indicate that apache was still running. Using pidof gives a list of pids if its running and nothing if its not. It's exit status is 0 At least one program was found with the requested name. 1 No program was found with the requested name. Hence its perfect for my script. Thanks for the other suggestions too. On Tue Jan 10, O Plameras wrote: Use 'pidof' (PID of command). A small script to demonstrate how to check if /usr/sbin/apache-perl is running. #!/bin/sh RET=0 pidof /usr/sbin/apache-perl /dev/null 2/dev/null RET=$? [ $RET -eq 0 ] echo Running exit 0 echo Not Running exit 1 To distinguished among processes (same name) save output from 'pidof' command (instead of dropping off to /dev/null) and process. This is going to be a bit involved. A simpler way is to copy apache-perl into different directories and use, pidof /dir1/apache-perl pidof /dir2/apache-perl etc. by using the method in the script above. -- -- Mike Lake Caver, Linux enthusiast and interested in anything technical. -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
Re: [SLUG] bash question - how to tell if apache has stopped?
On Mon, Jan 09, 2006 at 10:30:28AM +1100, Michael Lake wrote: Also the script will eventually need to be able to test for either the first apache-perl or the second one. At present the above script cannot descriminate between the two. i.e. # www-data 28319 28312 0 13:02 pts/300:00:00 /usr/sbin/apache-perl # root 28584 1 6 13:03 pts/300:00:00 /usr/sbin/apache-perl -f /etc/apache-perl/httpd2.conf What do folks use to determine if a process is running? Hi, pidof(8). -- Rene Cunningham DCLabs Pty Ltd http://www.dclabs.com.au -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
Re: [SLUG] bash question - how to tell if apache has stopped?
It depends how you start it, but generally speaking most daemons will have a pid file in /var/run/ (this is system dependant) - so if, for example, the file, /var/run/apache.pid exists, you can assume that the daemon is either running or has been killed ungracefully (eg, kill -9). If you're feeling real paranoid, you can check that this pid exists and is the apache process APACHEPID=/var/run/apache.pid if [ -f $APACHEPID ] then echo Apache did not stop exit 1 else echo Apache stopped, proceeding proceed fi On Mon, 2006-01-09 at 22:48 +1100, Ben Donohue wrote: Hi Mike, I do the following on a ppp script to check if the process is running or not. It will start the process if it has stopped. Maybe you can modify it for your needs... ps ax|fgrep pppd|fgrep -v fgrep /dev/null || /usr/sbin/pppd Ben Michael Lake wrote: Hi all I have a bash script that shuts down apache so that users can't updates a database, I do a backup of the database, then startup apache again. Roughly this is what I have: sudo apache-perlctl stop $LOGFILE 2 $ERRORFILE sleep 5 The sleep is to give apache enough time to shutdown but I would prefer to actually test if apache has shutdown and exit with an error or call an error function if it does not shutdown for some reason. Or it might just take 6 seconds to shutdown. So far I have tried things like this: -- #!/bin/bash function report_error { echo Error apache didn't stop exit 1 } function proceed { echo Apache stopped OK exit 0 } # The line below produces 'apache-perl' if apache-perl is running and '' if it's not. string=`ps -C apache-perl -o comm | uniq | grep -v COMMAND` if [ $string == 'apache-perl' ]; then # apache is still running report_error else # apache has stopped proceed fi -- I feel the way to get the $string is messy and may not be very portable. Also the script will eventually need to be able to test for either the first apache-perl or the second one. At present the above script cannot descriminate between the two. i.e. # www-data 28319 28312 0 13:02 pts/300:00:00 /usr/sbin/apache-perl # root 28584 1 6 13:03 pts/300:00:00 /usr/sbin/apache-perl -f /etc/apache-perl/httpd2.conf What do folks use to determine if a process is running? Mike -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
Re: [SLUG] bash question - how to tell if apache has stopped?
This one time, at band camp, Michael Lake wrote: What do folks use to determine if a process is running? service x status ps -C process pidof kill -0 $pid I use the last one in a test harness to wait until postgres has started up: # this am teh sucks i=0 max=60 until $PSQL -X -h $work template1 /dev/null 2/dev/null ; do i=$(($i + 1)) if [ $i -ge $max ]; then break fi if kill -0 $postmaster_pid /dev/null 21 ; then : still starting up else break fi sleep 1 done if ! kill -0 $postmaster_pid /dev/null 21 ; then cat $work/postmaster.log no_result fi (probably doens't answer your question though :) -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
Re: [SLUG] bash question - how to tell if apache has stopped?
On Mon, 2006-01-09 at 22:48 +1100, Ben Donohue wrote: ps ax|fgrep pppd|fgrep -v fgrep /dev/null || /usr/sbin/pppd Just a neat little trick that I picked up... on this list, I think, some time in the dim past. You can put square brackets around some of the process name you're looking for to stop the fgrep command from appearing in the results. ie: `fgrep pppd|fgrep -v fgrep` can be replaced with just `fgrep [p]ppd` Cheers, -- Pete -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
[SLUG] bash question - how to tell if apache has stopped?
Hi all I have a bash script that shuts down apache so that users can't updates a database, I do a backup of the database, then startup apache again. Roughly this is what I have: sudo apache-perlctl stop $LOGFILE 2 $ERRORFILE sleep 5 The sleep is to give apache enough time to shutdown but I would prefer to actually test if apache has shutdown and exit with an error or call an error function if it does not shutdown for some reason. Or it might just take 6 seconds to shutdown. So far I have tried things like this: -- #!/bin/bash function report_error { echo Error apache didn't stop exit 1 } function proceed { echo Apache stopped OK exit 0 } # The line below produces 'apache-perl' if apache-perl is running and '' if it's not. string=`ps -C apache-perl -o comm | uniq | grep -v COMMAND` if [ $string == 'apache-perl' ]; then # apache is still running report_error else # apache has stopped proceed fi -- I feel the way to get the $string is messy and may not be very portable. Also the script will eventually need to be able to test for either the first apache-perl or the second one. At present the above script cannot descriminate between the two. i.e. # www-data 28319 28312 0 13:02 pts/300:00:00 /usr/sbin/apache-perl # root 28584 1 6 13:03 pts/300:00:00 /usr/sbin/apache-perl -f /etc/apache-perl/httpd2.conf What do folks use to determine if a process is running? Mike -- Mike Lake Caver, Linux enthusiast and interested in anything technical. -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
Re: [SLUG] bash question - how to tell if apache has stopped?
Hi Mike, I do the following on a ppp script to check if the process is running or not. It will start the process if it has stopped. Maybe you can modify it for your needs... ps ax|fgrep pppd|fgrep -v fgrep /dev/null || /usr/sbin/pppd Ben Michael Lake wrote: Hi all I have a bash script that shuts down apache so that users can't updates a database, I do a backup of the database, then startup apache again. Roughly this is what I have: sudo apache-perlctl stop $LOGFILE 2 $ERRORFILE sleep 5 The sleep is to give apache enough time to shutdown but I would prefer to actually test if apache has shutdown and exit with an error or call an error function if it does not shutdown for some reason. Or it might just take 6 seconds to shutdown. So far I have tried things like this: -- #!/bin/bash function report_error { echo Error apache didn't stop exit 1 } function proceed { echo Apache stopped OK exit 0 } # The line below produces 'apache-perl' if apache-perl is running and '' if it's not. string=`ps -C apache-perl -o comm | uniq | grep -v COMMAND` if [ $string == 'apache-perl' ]; then # apache is still running report_error else # apache has stopped proceed fi -- I feel the way to get the $string is messy and may not be very portable. Also the script will eventually need to be able to test for either the first apache-perl or the second one. At present the above script cannot descriminate between the two. i.e. # www-data 28319 28312 0 13:02 pts/300:00:00 /usr/sbin/apache-perl # root 28584 1 6 13:03 pts/300:00:00 /usr/sbin/apache-perl -f /etc/apache-perl/httpd2.conf What do folks use to determine if a process is running? Mike -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
Re: [SLUG] Bash Question - Redirection of output determined by Variable name
On Fri, Aug 05, 2005 at 08:01:02AM +1000, James Gray wrote: I thought /dev/stdout and /dev/stderr were the POSIX-ly correct forms? I've always used them in my scripts for this sort of thing and often move them from Linux - Solaris - BSD - AIX without needing much more than editing the #! line. Yeah, I remembered those just after sending this message, and thought they might always go to the real stdout/stderr. i.e. if redirected, though I'm not sure that makes sense. But it turns out these are just links to /dev/fd/1 and /dev/fd/2. On Linux at least. Likewise /dev/stdin - fd/0 Matt -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
Re: [SLUG] Bash Question - Redirection of output determined by Variable name
On Fri, Aug 05, 2005 at 09:59:21AM +1000, Ben Stringer wrote: I've always just used the tty command to return the connected tty. This seems to work consistently across many unices. Yeah, but it be extremely annoying to deal with a program that insists on a tty. /dev/stdout,stderr,stdin is a better bet. Matt -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
Re: [SLUG] Bash Question - Redirection of output determined by Variable name
On Thu, Aug 04, 2005 at 03:00:49PM +1000, [EMAIL PROTECTED] wrote: The $J substitution into the last command works fine but the $R bit, which attempts to redirect the output to a file, does not. Bash seems to interpret the bit as part of the command rather than a redirection instruction. Read about expansion in the bash manual. What you want to do is eval the command. e.g. #!/bin/bash output= /tmp/output eval ls $output -i [EMAIL PROTECTED] http://www.gelato.unsw.edu.au signature.asc Description: Digital signature -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
Re: [SLUG] Bash Question - Redirection of output determined by Variable name
Hi, From: [EMAIL PROTECTED] Date: Thu, 4 Aug 2005 15:00:49 +1000 I tried the following if [ $1 = quiet ]; then R= /home/steven/mfgbacker/serr 21 else R= fi [...snip...] DTEN=`date +%Y.%m.%d %H:%M:%S` echo inside makedvd $DTEN $R I=`mkisofs -R -J -q -print-size /back1/tarback/*.tgz` J=tsize=${I}s mkisofs -R -J -q -o /back1/tarback/dvd.iso /back1/tarback/*.tgz $R cdrecord -sao $J dev=ATAPI:0,0,0 speed=2 driveropts=burnfree /back1/tarback/dvd.iso $R [...snip...] The $J substitution into the last command works fine but the $R bit, which attempts to redirect the output to a file, does not. Bash seems to interpret the bit as part of the command rather than a redirection instruction. My suggestion is using exec commoand to change output. set_output () { if $QUIET ; then exec 31 42 /home/steven/mfgbacker/serr 21 fi } reset_output () { if $QUIET ; then exec 13 24 3- 4- fi } if [ $1 = quiet ]; then QUIET=/bin/true else QUIET=/bin/false fi (snip) set_output mkisofs -R -J -q -o /back1/tarback/dvd.iso /back1/tarback/*.tgz reset_output -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
Re: [SLUG] Bash Question - Redirection of output determined by Variable name
On Thu, Aug 04, 2005 at 04:18:21PM +1000, Ian Wienand wrote: e.g. #!/bin/bash output= /tmp/output eval ls $output Just don't put the in the var. If you want it to go the screen, you can use /dev/tty. Or for those unixes that support it, like linux, /dev/fd/1 is stdout. So: R=/dev/tty (or R=/dev/fd/1) or R=/tmp/output then somecommand $R -- Matt -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
Re: [SLUG] Bash Question - Redirection of output determined by Variable name
On Thu, 2005-08-04 at 15:00 +1000, [EMAIL PROTECTED] wrote: I have a bash script sometimes called by another script and sometimes run from the command line. I would like standard output (and error) to go to the screen if I run the script from the keyboard and to a nominated file if I call it from another script. in the calling script QUIET=YES myscript my options in the script if [ $QUIET = YES ] ; then X=something else X= fi Easy really. -- Ken Foskey OpenOffice.org developer -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
Re: [SLUG] Bash Question - Redirection of output determined by Variable name
On Thursday 04 August 2005 21:45, Matthew Hannigan wrote: On Thu, Aug 04, 2005 at 04:18:21PM +1000, Ian Wienand wrote: e.g. #!/bin/bash output= /tmp/output eval ls $output Just don't put the in the var. If you want it to go the screen, you can use /dev/tty. Or for those unixes that support it, like linux, /dev/fd/1 is stdout. I thought /dev/stdout and /dev/stderr were the POSIX-ly correct forms? I've always used them in my scripts for this sort of thing and often move them from Linux - Solaris - BSD - AIX without needing much more than editing the #! line. Cheers, James -- All my life I wanted to be someone; I guess I should have been more specific. -- Jane Wagner -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
Re: [SLUG] Bash Question - Redirection of output determined by Variable name
On Fri, 2005-08-05 at 08:01 +1000, James Gray wrote: On Thursday 04 August 2005 21:45, Matthew Hannigan wrote: On Thu, Aug 04, 2005 at 04:18:21PM +1000, Ian Wienand wrote: e.g. #!/bin/bash output= /tmp/output eval ls $output Just don't put the in the var. If you want it to go the screen, you can use /dev/tty. Or for those unixes that support it, like linux, /dev/fd/1 is stdout. I thought /dev/stdout and /dev/stderr were the POSIX-ly correct forms? I've always used them in my scripts for this sort of thing and often move them from Linux - Solaris - BSD - AIX without needing much more than editing the #! line. I've always just used the tty command to return the connected tty. This seems to work consistently across many unices. Eg. if [ ${USE_SCREEN} = YES ] then OUTPUT_STREAM=`tty` else OUTPUT_STREAM=/tmp/my_output_file fi echo Hello ${OUTPUT_STREAM} 21 Checking the return code of tty -s can also be a good way to see if a script has no connected tty, like the case where it has been called from cron. Cheers, Ben -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
[SLUG] Bash Question - Redirection of output determined by Variable name
I have a bash script sometimes called by another script and sometimes run from the command line. I would like standard output (and error) to go to the screen if I run the script from the keyboard and to a nominated file if I call it from another script. I tried the following if [ $1 = quiet ]; then R= /home/steven/mfgbacker/serr 21 else R= fi [...snip...] DTEN=`date +%Y.%m.%d %H:%M:%S` echo inside makedvd $DTEN $R I=`mkisofs -R -J -q -print-size /back1/tarback/*.tgz` J=tsize=${I}s mkisofs -R -J -q -o /back1/tarback/dvd.iso /back1/tarback/*.tgz $R cdrecord -sao $J dev=ATAPI:0,0,0 speed=2 driveropts=burnfree /back1/tarback/dvd.iso $R [...snip...] The $J substitution into the last command works fine but the $R bit, which attempts to redirect the output to a file, does not. Bash seems to interpret the bit as part of the command rather than a redirection instruction. I am not sure if this is possible in bash or not. There are lots of other commands in the script which also redirect so I would rather not do something like: if [$1 = quiet]; then mkisofs -R -J -q -o /back1/tarback/dvd.iso /back1/tarback/*.tgz /home/steven/mfgbacker/serr 21 else mkisofs -R -J -q -o /back1/tarback/dvd.iso /back1/tarback/*.tgz fi because I would have to do it many times. I think the problem is something to do with when bash is processing the substitution. I have tried various combinations of different quotes without success. Could someone please point me in the right direction Thank you and regards Steven -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html