2

I’m trying to create an alias to open up my log fie

alias open_log='date=`date +%y%m%d`;sudo tail -n 10 ~/logs/reconfig-$date.log;'

When I run that alias

open_log

I got

tail: cannot open ‘/home/benu/logs/reconfig-.log’ for reading: No such file or directory

But if I run

"what I set for that alias"

date=`date +%y%m%d`;sudo tail -n 10 ~/logs/reconfig-$date.log;

It works perfectly fine.

+ service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables: [  OK  ]
+ mkdir -p /etc/nginx/sites-available
+ cp /root/portal-data/sites-available/default-https /etc/nginx/sites-available/default
+ '[' '!' -f /etc/ssl/mywifibusiness.trg.telenet.be.cert.pem ']'
+ echo 'Error: Unable to find certificate at /etc/ssl/mywifibusiness.trg.telenet.be.cert.pem'
Error: Unable to find certificate at /etc/ssl/mywifibusiness.trg.telenet.be.cert.pem
+ exit 1
Error: Executing /home/benu/reconfig.d/70-ssc-portal.sh returned 1
Reconfigure aborted

Why they’re behaving different ? Can anyone elaborate ?

How do I fix my alias to make it work ?

  • 1
    alias open_log="sudo tail -n 10 ~/logs/reconfig-$(date +%y%m%d).log" – Christopher May 4 '17 at 14:50
  • 1
    The only system I have access to is an Ubuntu system, and this seems to work there. @Christopher I don't think OP wants weak quotes here – Fox May 4 '17 at 14:55
  • I even tried date=date +%y%m%d; echo -e "~/logs/reconfig-$date.log" | sudo tail -n 10; , but it does is printing out my path command, rather than running that command. How do I fix it guys ? – kyo May 4 '17 at 14:56
  • Works for me, though setting a variable in an alias a bit weird (it stays set in the outside context). Do you have an alias or function overriding date or something like that? – ilkkachu May 4 '17 at 15:01
  • 1
    Why would you want to open a file in your home directory as root? Why not just alias open_log="tail -n 10 ~/logs/reconfig-$(date +%y%m%d).log;"? – terdon May 4 '17 at 15:02
2

Sorry, @ihue. I got this wrong to start. Kudos to @ilkkachu.

Compare the alias with the command line, posted before the edit:

alias 'date=date +%y%m%d;sudo tail -n 10 ~/logs/reconfig-$date.log;'
date=`date +%y%m%d`;sudo tail -n 10 ~/logs/reconfig-$date.log;

The alias is missing the backticks. Also, the backticks are all but antiquated. Use the following form for command substitution: $(command).

Double quotes work as follows, which sets the date each time the alias was set.

alias open_log="sudo tail -n 10 ~/logs/reconfig-$(date +%y%m%d).log"

Single quotes work dynamically, setting the date each time you call the alias.

alias open_log='sudo tail -n 10 ~/logs/reconfig-$(date +%y%m%d).log'
  • 1
    That alias, with the double-quotes, would expand the command substitution at the time the alias is set, not when it's executed. – ilkkachu May 4 '17 at 15:01
  • Please note the accurate comment by @ilkkachu. In other words, every time you source your aliases, however that may be - at log in time or manually, perhaps - the date is set at that time, not dynamically every time you call the alias. – Christopher May 4 '17 at 15:04
  • 1
    ...of course with single-quotes it would be the opposite, compare alias d1='echo $(date +%T)' d2="echo $(date +%T)" – ilkkachu May 4 '17 at 15:19
  • Thank you @ilkkachu. I had the wrong understanding to start. – Christopher May 4 '17 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.