home directory

hello

i want shell script.

as root , i want to untar specific.tar.gz to all home user directory

and after untar , there is 1.txt 2.txt ~~ 26.txt in/public_html/test1/
i want randomly selected 6 text files in 1.txt 2.txt ~26.txt to be renamed
newword1.word , newword2.word , ~~ newword6.word .

and change all file chown user1:user1
user2:user2 .........

and chmod all files 755

/home/user1/public_html/
/home/user2/public_html/
/home/user3/public_html/
/home/user4/public_html

.
.
/home/user99/public_html/

My preferred method of dealing with things like this is to do in 2 or 3 steps,
not all at once.

Here's the first script:

#!/bin/ksh

#----------------------------------------------------------------------#
# head to home partition.                                              #
#----------------------------------------------------------------------#
cd /home

/bin/ls |
while read dir ; do

#----------------------------------------------------------------------#
# Not a directory, next....                                            #
#----------------------------------------------------------------------#
  if [ ! -d $dir ]; then
    continue
  fi

  cd $dir

#----------------------------------------------------------------------#
# Untar the tar file....                                               #
#----------------------------------------------------------------------#
  cp /fullpath/tar_file.gz .
  gunzip tar_file.gz

  chmod -R *

  cd /home

done | tee /tmp/log1

Once that thing is debugged and working correctly,
I use a code generator to create the next dangerous type commands:
( mv commands in this case.... )

#!/bin/ksh

#----------------------------------------------------------------------#
# head to home partition.                                              #
#----------------------------------------------------------------------#
cd /home

/bin/ls |
while read dir ; do

  cd /home/$dir/public_html/test1

#----------------------------------------------------------------------#
# Randomly rename 6 files.                                             #
#----------------------------------------------------------------------#
  num=0
  /bin/ls *.txt |
  while read file_nm ; do

    (( check = $RANDOM % 2 ))

    if [ $check -eq 0 ]; then
      continue
    fi

    (( num = num + 1 ))

    echo /bin/mv $PWD/$file_nm $PWD/newword${num}.word

    if [ $num -ge 6 ]; then
      break
    fi
  done ### while doing random renaming....

  cd /home

done |
  tee /tmp/log2

Then, before executing, I examine the contents of /tmp/log2.
If the commands in there look right --- then I execute that script with:

/bin/ksh -xvf /tmp/log2 2>&1 | tee /tmp/log3

Using this method decreases the risk incurred at each step and
provides a log for each step also.

HTH

Why are you using ls? And why a full path?

for dir in */

Why copy the tarball?

That only ungzips it; it doesn't untar the files.

tar xvzf /fullpath/tar_file.gz

That command is missing the permissions.

As above: why ls?

Yet again.

for file_nm in *.txt

Why not use the standard syntax?

check=$(( $RANDOM % 2 ))

(Though $RANDOM is not standard)

why not ls? are you worried about performance again?

fullpaths are safest and using them means you're less likely to put something
somewhere unintended.

oops:

chmod -R 755 *

Preferred would be to unzip the tarball elsewhere, I suppose, then
get fancy with the tar -xvf command and redirection.

I've had other posters complain when I use your syntax that it's not standard.
So. I give up. Let the original poster do some of the work. It won't kill them.

deleted

not on my machine...

Not only that, but the way you used it could break the script with some filenames.

There's nothing safer about full paths, but they are less portable.

#!/bin/ksh
while read line
do
cd $line/radmin
num=0
/bin/ls *.txt |
while read file_nm ; do

\(\( check = $RANDOM % 2 \)\)

if [ $check -eq 0 ]; then
  continue
fi

\(\( num = num \+ 1 \)\)

echo /bin/cp $PWD/$file_nm $PWD/newword$\{num\}.word

if [ $num -ge 6 ]; then
  break
fi

done ### while doing random renaming....

done < source.txt

newword.word file didnt be created.

Then they are not using a standard Unix shell. (Can you give any examples?)

The Unix shell is clearly defined by the Open Group at sh, shell, the standard command language interpreter.