7

I write expect script that login to remote machine and run there some scripts But I need also to verify the following

Verify if directory /var/cti/adm/APP exists.

If APP not exists under adm directory , then need to create this directory and add ownership to this directory , ( as chown system )

Please advice how to check if directory exist in expect script and if not need to create this directory

example of part of my expect script

 #!/usr/bin/expect -f
 set multiPrompt {[#>$]}
 send "ssh  $LOGIN3@$IP\r"
 sleep 0.5
       expect  {
       word:  {send $PASS\r ; exp_continue } 
       expect -re $multiPrompt
       }

example how we can do it with bash

 [[ ! -d /.../..../... ]] && mkdir -p xxxxx
Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
maihabunash
  • 6,973
  • 19
  • 64
  • 80
  • 2
    I'm confused. Just plain `mkdir` creates a directory if it doesn't exist and does nothing otherwise. You can pass `--parents` to ensure the whole chain of nested directories exists too. Why do you need a whole script for that? – Anko - inactive in protest Sep 02 '14 at 13:40
  • I cant just send mkdir , because I want to create dir only in case dir not exsits !!!!!!!!!!!!! – maihabunash Sep 02 '14 at 13:43
  • 3
    That's what I mean. If I run `mkdir ~/test` and I already have a directory called `~/test`, nothing happens. If I don't, it gets created. Did you mean something else? – Anko - inactive in protest Sep 02 '14 at 13:47

2 Answers2

11

You can do this in pure Tcl without exec:

#!/usr/bin/env expect
set path dir1/dir2/dir3
file mkdir $path ;# [file mkdir] in Tcl is like mkdir -p
file attributes $path -owner system
nwk
  • 979
  • 6
  • 17
0

You can execute the mkdir command as a subprocess.

exec mkdir -p -- $directory

If you need to set the ownership as well but only when the directory is created, invoke a shell explicitly.

exec sh -c "if ! [ -d \"\$1\" ]; then mkdir -- \"\$1\" && chown -- bob \"\$1\"; fi" _ $directory
Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175