Linux Bash Shell Script: How to "ls" a directory and check some output string? -


i'm newbie linux shell scripting. need know is, in command line, use:

# ls /var/log audit            consolekit     cups        maillog           messages           ntpstats  secure-20130616   spooler-20130623  vsftpd.log-20130616 boot.log         cron           dmesg       maillog-20130616  messages-20130616  prelink   secure-20130623   spooler-20130701  vsftpd.log-20130623 ... . . . .. 

then

# ls /var/aaaaaaaaaaaaaaa ls: cannot access /var/aaaaaaaaaaaaaaa: no such file or directory 

so shell script:

  • how can run command: # ls /var/aaaaaaaaa
  • and detect if there output string ls: cannot access or not?

note: may ask me whether want detect failure. or output string. i'm keen know both way. thank you.

to check directory:

if [ ! -d '/var/aaaaaaa' ];    echo 'no dir!' fi 

for file:

if [ ! -f '/var/aaaaaaa' ];    echo 'no file!' fi 

to check output:

if ls '/var/aaaaaaa' 2>&1 | grep 'no such';      echo 'no such';  fi 

to check when ls fails:

if ! ls '/var/aaaaaaa' &> /dev/null;    echo 'failed' fi 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -