Sponsored Content
Top Forums Shell Programming and Scripting Bash script - ENV Variable context problem using su Post 302870409 by jcdole on Sunday 3rd of November 2013 01:08:42 PM
Old 11-03-2013
Bash script - ENV Variable context problem using su

Hello
I have found some piece of code to verify and then run shell script with root permission from normal user.
see : http://blog.mecworks.com/articles/20...script-as-root
I have wrote two scripts using this tips.
- one to copy file from server to local computer ( files_copy_from_server.sh )
and this program is started by using another script ( files_copy_from_server_launcher.sh)
- one to copy file to server from local computer ( files_copy_to_server.sh )
and this program is started by using another script ( files_copy_to_server_launcher.sh)

The first one run perfectly.
Code:
user@asus:~> /home/user/bin/files_copy_from_server_launcher
CMD   : /home/user/bin/files_copy_from_server
PARAM : /root/bin/mysql* /home/user/
Please enter root's password.
Password: 
MAIN - there are  2 parametres
MAIN - PARAM0 : /home/juser/bin/files_copy_from_server
MAIN - PARAM1 : /root/bin/mysql*
MAIN - PARAM2 : /home/user/
COMMAND : pscp -v -2 -P 12345 -i /root/.ssh/my-key.ppk -l root   root@192.168.123.123:/root/bin/mysql*   /home/user/
Server version: SSH-2.0-OpenSSH_6.1
Using SSH protocol version 2
Doing Diffie-Hellman group exchange
Doing Diffie-Hellman key exchange with hash SHA-256
Host key fingerprint is:
ssh-rsa 2048 db:dc:dd:de:df:1a:1b:1c:1d:1e:c0:c1:c2:c3:c4:c5
Initialised AES-256 SDCTR client->server encryption
Initialised HMAC-SHA1 client->server MAC algorithm
Initialised AES-256 SDCTR server->client encryption
Initialised HMAC-SHA1 server->client MAC algorithm
Reading private key file "/root/.ssh/my-key.ppk"
Pageant is running. Requesting keys.
Pageant has 0 SSH-2 keys
Configured key file not in Pageant
Using username "root".
Offered public key
Offer of public key accepted
Authenticating with public key "imported-openssh-key"
Sent public key signature
Access granted
Opened channel for session
Started a shell/command
Using SFTP
Connected to 192.168.123.123

mysql_04_make-mysql-from- | 0 kB |   0.9 kB/s | ETA: 00:00:00 | 100%
mysql_install_db_mysql_si | 2 kB |   2.5 kB/s | ETA: 00:00:00 | 100%
mysql_remove_suse_mysql_a | 1 kB |   1.4 kB/s | ETA: 00:00:00 | 100%
mysql_create_users_mysql  | 2 kB |   2.6 kB/s | ETA: 00:00:00 | 100%
mysql_create_dir_mysql    | 2 kB |   2.3 kB/s | ETA: 00:00:00 | 100%
mysql_remove_oracle_mysql | 1 kB |   1.8 kB/s | ETA: 00:00:00 | 100%
mysql_01_install_mysql_ed | 1 kB |   1.5 kB/s | ETA: 00:00:00 | 100%
mysql_remove_oracle_mysql | 1 kB |   1.4 kB/s | ETA: 00:00:00 | 100%
mysql_install_mysql_softw | 2 kB |   2.9 kB/s | ETA: 00:00:00 | 100%
mysql_create_mysql_config | 4 kB |   4.9 kB/s | ETA: 00:00:00 | 100%
mysql_full_install_mysql  | 3 kB |   3.2 kB/s | ETA: 00:00:00 | 100%
mysql_03_my_function      | 4 kB |   4.7 kB/s | ETA: 00:00:00 | 100%
mysql_remove_mariadb___   | 1 kB |   1.6 kB/s | ETA: 00:00:00 | 100%
mysql_create_mysql_config | 3 kB |   3.5 kB/s | ETA: 00:00:00 | 100%
mysql_install_db_mysql_mu | 2 kB |   2.2 kB/s | ETA: 00:00:00 | 100%
mysql_remove_suse_mysql__ | 1 kB |   1.6 kB/s | ETA: 00:00:00 | 100%
mysql_remove_mariadb_and_ | 1 kB |   1.4 kB/s | ETA: 00:00:00 | 100%
mysql_02_install_oracle_m | 6 kB |   6.9 kB/s | ETA: 00:00:00 | 100%
Sent EOF message
Server sent command exit status 0
Disconnected: All channels closed
user@asus:~>

The second one failed because variables are not set correctly :

Code:
user@asus:~> /home/user/bin/files_copy_to_server_launcher
CMD   : /home/user/bin/files_copy_to_server
PARAM : /home/user/bin/files_copy* /root/bin/
Please enter root's password.
Password: 
MAIN - il y a 6 parametres
MAIN - PARAM0 : /home/user/bin/files_copy_to_server  - correct
MAIN - PARAM1 : /home/user/bin/files_copy_from_server - should be "/home/user/bin/files_copy*"
MAIN - PARAM2 : /home/user/bin/files_copy_from_server_launcher - should be "/root/bin/"
MAIN - PARAM3 : /home/user/bin/files_copy_to_server - should not exists
MAIN - PARAM4 : /home/user/bin/files_copy_to_server.1 - should not exists
MAIN - PARAM5 : /home/user/bin/files_copy_to_server_launcher - should not exists
USAGE : /home/user/bin/files_copy_to_server  local_source_file  server_file_dest
user@asus:~>

Any help is welcome
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

bash env variable containing @

I want to set a bash env variable which has @ in its name, for example, @YOGESH@ may i know how do i do this? (4 Replies)
Discussion started by: Yogesh Sawant
4 Replies

2. Shell Programming and Scripting

passing variable from bash to perl from bash script

Hi All, I need to pass a variable to perl script from bash script, where in perl i am using if condition. Here is the cmd what i am using in perl FROM_DATE="06/05/2008" TO_DATE="07/05/2008" "perl -ne ' print if ( $_ >="$FROM_DATE" && $_ <= "$TO_DATE" ) ' filename" filename has... (10 Replies)
Discussion started by: arsidh
10 Replies

3. UNIX for Advanced & Expert Users

context lost problem

there are several same servers(process) on more than one server(machine) providing the same service. we store session/context within the server(process), if the same client login, he will be directed to the very server service for him last time. But, if a server(machine or process) down, the... (1 Reply)
Discussion started by: zhongyj
1 Replies

4. Shell Programming and Scripting

get env variable from last script

I have 2 scripts t2.sh calls t1.sh. I need to get the vaule of a env variable from t1.sh /tmp/test$ cat t1.sh #!/bin/sh INSTANCE="font/fc-cache" export INSTANCE svcadm disable ${INSTANCE} /tmp/test$ cat t2.sh #!/bin/sh . /tmp/test/t1.sh echo ${INSTANCE} The above works... (9 Replies)
Discussion started by: honglus
9 Replies

5. Shell Programming and Scripting

Set/Export Env Vars from with Shell Script With Input Variable

I have a shell script I want to run that will set environment variables based on the value of an input variable submitted when the shell script is called. For example: $ mgenv.sh prod This would set environment variables for prod $ mgenv.sh test This would set environment variables... (1 Reply)
Discussion started by: brtaylor73
1 Replies

6. UNIX for Advanced & Expert Users

problem in Unix Env. in Shell script

sdir;csp os_lib-0.5.24;bdir;cbpdob ---enable-useosstl os_lib-0.5.24;mbp os_lib-0.5.24; If i run this command in unix shell directly it is running. sdir;csp HA_util-0.0.7;bdir;cbpdob ---enable-useosstl HA_util-0.0.7;mbp HA_util-0.0.7; HA_util === Configuring source package HA_util... (4 Replies)
Discussion started by: girija
4 Replies

7. Shell Programming and Scripting

how to set/get shell env variable in python script

greetings, i have a sh script that calls a python script. the sh script sets an env variable BIN: export BIN=bin64i need to get that BIN variable's value and use it within this python script. anyone know how to do this? thanx in advance. (5 Replies)
Discussion started by: crimso
5 Replies

8. Web Development

Deny from env=env-variable Does not work

(Above from Apache docs). On my system, using: SetEnvIf User-Agent Mozilla IsBad=1 Order allow,deny Allow from all Deny from env=IsBad ...I see that environment variable is set (using phpinfo()) but the page is still served. No errors in the Apache logs. (1 Reply)
Discussion started by: gnurob
1 Replies

9. Shell Programming and Scripting

Bash, Dynamic Variable Problem >.<

Hello, I have a problem with a bash script, I've been doing recherches but i can't make it work. It is my first time with a dynamic variable and i don't understand how to write it. #!/bin/bash USER=BARSPIN HOTKEYS_PATH="/home/$USER/Documents/bash/tibia/HOTKEYS" CFG1="A" CFG2="B"... (5 Replies)
Discussion started by: barspin
5 Replies

10. Shell Programming and Scripting

BASH script to export var to env

Hi all I am trying to create a script that takes a password input then writes that to a tmp file and puts that tmp file path in my env as a var. It does everything but export the my env and I am unsure why. I am using Ubuntu 12.4 #!/bin/bash read -s -p "Enter Password: " gfpassword... (5 Replies)
Discussion started by: koikoi
5 Replies
All times are GMT -4. The time now is 08:10 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy