Home > Blockchain >  Create an array of paths in Bash
Create an array of paths in Bash

Time:01-18

I need to create a script to perform a CLI command inside each sub directory Imagine I've the main direct and inside 3 sub-directories. I need to loop in each sub-directories and perform an action

In PHP I wilt try something like

foreach( $paths as $path){
   //do something
}

My full script (from comment)

#!/bin/bash 

path=/home/sebastien/projets/XXXX/customers/Sites/ 
today=$(date  %s) 

for d in $path; do 
  [ -L "${d%/}" ] && continue
  echo "$d" 
  orig=$(stat -c %y ${d} ) 
  epoch=$(date -d "${orig}"  "%s") 
  diff=$(($today-$epoch)) 
  jour=$(($diff/86400)) 
  echo $jour 
  if [ "$jour" -ge 4 ] then 
    cd $d #lando rebuild --y
  fi
done

How to perform this in bash script please ?

CodePudding user response:

You can do something like

paths=(dir1 dir2)
for path in "${paths[@]}"
do
    echo "$path"
done

CodePudding user response:

I need to loop in each sub-directories and perform an action

Then do that.

for i in */; do
  action "$i"
done

CodePudding user response:

With GNU bash version >= 4.0:

# Enable globstar feature
shopt -s globstar

# glob for all directories recursive and remove trailing /
for dir in **/; do echo "${dir/%\//}"; done

# Disable globstar feature
shopt -u globstar

See: man bash

  •  Tags:  
  • Related