Hello.
I am running 2 scripts : script_1 and script_2
These scripts are run as root
Script 2 contains :
#!/bin/bash
#
# ~/bin/script_2
#
E_BAD_PARAM=115
#
date2stamp () {
date --date "$1" +%Y-%m-%d___%H:%M:%S
}
#
USER_NAME=$1
NB_PARAM=$#
PARAM0=$0
if [ $NB_PARAM -ne 1 ] ; then
echo "Usage : ~/bin/script_2 user_name"
exit $E_BAD_PARAM
fi
#
echo "We are in script 2"
#
TIMESTAMP=$(date2stamp now)
#
if [ -f /home/$USER_NAME/.ssh/id_rsa -a -f /home/$USER_NAME/.ssh/id_rsa.pub ] ; then
echo "SSH Keys exists for user : $USER_NAME ."
echo "Hit CTRL-C to abort"
echo "Anything else to continue and create a new key for that user"
read A_LINE
echo "Creating the key ...."
mv /home/$USER_NAME/.ssh/authorized_keys /home/$USER_NAME/.ssh/authorized_keys.$TIMESTAMP
mv /home/$USER_NAME/.ssh/id_rsa /home/$USER_NAME/.ssh/id_rsa.$TIMESTAMP
mv /home/$USER_NAME/.ssh/id_rsa.pub /home/$USER_NAME/.ssh/id_rsa.pub.$TIMESTAMP
fi
#
echo "Done."
When calling from a console
linux:~/bin # ~/bin/script_2 user_test
I get :
linux:~/bin # ~/bin/script_2 user_test
We are in script 2
SSH Keys exists for user : user_test .
Hit CTRL-C to abort
Anything else to continue and create a new key for that user
and the program wait until I hit enter or CTRL-C
Now this script is call from script_1
#!/bin/bash
#
# ~/bin/script_1
#
############################################
#
~/bin/script_2 user_test
When calling from a console
linux:~/bin # ~/bin/script_1
I get :
linux:~/bin # ~/bin/script_1
We are in script 2
SSH Keys exists for user : user_test .
Hit CTRL-C to abort
Anything else to continue and create a new key for that user
and the program wait until I hit enter or CTRL-C
So same behavior.
If script_1 is :
#!/bin/bash
#
# ~/bin/script_1
#
############################################
#
for USER_NAME in toto tata user_test ; do
echo "User : $USER_NAME"
if [[ "$USER_NAME" != "user_test" ]] ; then
continue
fi
~/bin/script_2 "$USER_NAME"
done
Then calling script_1
linux:~/bin # ~/bin/script _1
give
linux:~/bin # ~/bin/script_1
We are in script 2
SSH Keys exists for user : user_test .
Hit CTRL-C to abort
Anything else to continue and create a new key for that user
and the program wait until I hit enter or CTRL-C
So same behavior.
In real life, the list of users come from a file
Now script_1 is :
#!/bin/bash
#
# ~/bin/script_1
#
############################################
#
while read USER_NAME ; do
echo "User : $USER_NAME"
if [[ "$USER_NAME" != "user_test" ]] ; then
continue
fi
~/bin/script_2 "$USER_NAME"
done < "~/list_user"
Then calling script_1
linux:~/bin # ~/bin/script _1
give
linux:~/bin # ~/bin/script_1
User : bidon
User : user_test
We are in script 2
SSH Keys exists for user : user_test .
Hit CTRL-C to abort
Anything else to continue and create a new key for that user
Creating the key ....
`/home/user_test/.ssh/authorized_keys' -> `/home/user_test/.ssh/authorized_keys.2012-10-28___15:50:57'
`/home/user_test/.ssh/id_rsa' -> `/home/user_test/.ssh/id_rsa.2012-10-28___15:50:57'
`/home/user_test/.ssh/id_rsa.pub' -> `/home/user_test/.ssh/id_rsa.pub.2012-10-28___15:50:57'
version: 1 subdir: 2012_10_25 DIR: /data_1/002_config_linux/install_2012_09_04/006_backup_current_conf_files
Generating public/private rsa key pair.
........
........
Done
Now script 2 does not wait for "enter" or "CTRL-C"
I think that the problem come from because there is 2 read commands in sequence
One to get user name one by one
And the second which wait for user input.
Any idea to make this working ?
---------- Post updated at 17:02 ---------- Previous update was at 16:24 ----------
Finally found info on
http://unix.stackexchange.com/questions/26601
Make change in script_1
before :
#!/bin/bash
#
# ~/bin/script_1
#
############################################
#
while read USER_NAME ; do
echo "User : $USER_NAME"
if [[ "$USER_NAME" != "user_test" ]] ; then
continue
fi
~/bin/script_2 "$USER_NAME"
done < "~/list_user"
after correction :
#!/bin/bash
#
# ~/bin/script_1
#
############################################
#
while read USER_NAME <&5 ; do
echo "User : $USER_NAME"
if [[ "$USER_NAME" != "user_test" ]] ; then
continue
fi
~/bin/script_2 "$USER_NAME"
done 5< "~/list_user"
Thread is closed.
Thank you for taking time to re'ad this thread