0

Hello I have one bash script like this :

readarray -t a < /home/z/Systeme/Sonde/dataUser.txt 
len=${#a[@]}
for((i=0; i<$len; i++));
do
   du -sh ~${a[i]}
done

The file dataUser .txt now only contain username of the session in this case only z is write in the file.

In the prompt when I do du -sh ~z The result is : 415M home/z So this is working

But when I start my script the prompt display : du : cannot access 'z' : No file or directory of this type. I think this is because of the type of {a[i]} How can I solve this ? Thanks in advance !

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • `home/z` is a path relative to your current directly. Use an abosolute path if you want to it to work the same from any directory. – jordanm Mar 09 '22 at 17:35
  • 1
    Note that `var=user; echo ~$var` is **not** the same as `echo ~user`. Tilde expansion happens **before** parameter expansion, so it can't take advantage of variable contents. Nothing discussed here is in any way specific to `du` -- all this processing happens in the shell before `du` is started at all. – Charles Duffy Mar 09 '22 at 17:40
  • (tagging note: Keep tags down to things that are specific to the question; this should only be tagged "du" if you can't cause the same problem with non-du commands; should only be tagged "linux" if you can't cause the problem on non-Linux operating systems; should only tag "arrays" if it doesn't happen when the username comes from a string variable; etc). – Charles Duffy Mar 09 '22 at 17:43
  • In addition to the linked duplicate also see [this](https://stackoverflow.com/a/53219743/14122) answer on [How to get $HOME directory of a different user in bash script?](https://stackoverflow.com/questions/20504662/how-to-get-home-directory-of-different-user-in-bash-script) – Charles Duffy Mar 09 '22 at 17:45

0 Answers0