Condition local or remote server command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Condition local or remote server command
# 1  
Old 03-01-2013
Condition local or remote server command

Hello,

Can you help me ?
$7 fits to remote server.
I can launch the script from local or remote server.
I would like my_script.sh to choose local or remote command depending the variable $7.
Is the function f1 right or wrong ? In this moment, i can't test it.
Thanks in advance.

I create the input file
Code:
vi my_inputfile
# comment 1
# comment 2
$1 $2 $3 $4 $5 $6 $7

Code:
./my_script my_inputfile

Code:
vi my_script

Code:
#!/bin/bash
# set -vx
f1()    {
          server=$(hostname -s)
          if [ $server = $6 ]; then
          echo $1 $2 $3 $4 $5 $6   
          else
          ssh -n $6 'echo $1 $2 $3 $4 $5 $6'                                   
        } 
case $# in
        0)      echo -e "# comment1\n# comment2" > list_file    
                read -p "Please fill in this list_file: "      
                echo $REPLY                                     
                ;;
        1)     if [ ! -f "$1" ] 
               then  > $1 && echo -e "# comment1\n# comment2" > $1
               read -p "Please fill in "$1": "
               echo $REPLY 
               else 
                   if [ -f "$1" -a $(grep -v '^#' "$1" | wc -l ) -ne 0 ]                             
                   then
                   grep -v '^#' "$1" |
                        while read arg1 arg2 arg3 arg4 arg5 arg6 arg7
                                do f1 $arg1 $arg2 $arg3 $arg4 $arg5 $arg6 $arg7
                                done  
                   else 
                   read -p "Please fill in $1 : "
                   echo $REPLY
                   fi 
               fi
               ;;  
        4)      echo -e "# comment1\n# comment2" > list_file    
                echo $1 $2 $3 $4 >> list_file                   
                ;;
        5)      echo -e "# comment1\n# comment2" > $1           
                f1 $2 $3 $4 $5 $6 >> "$1"                   
                ;;
        6)      echo -e "# comment1\n# comment2" > $1           
                f1 $2 $3 $4 $5 $6 $7 >> "$1"                   
                ;;
        *)      echo "error msg"; exit
esac


Last edited by amazigh42; 03-01-2013 at 02:16 PM..
# 2  
Old 03-01-2013
Until you test it , difficult to find if this is having any issue.

Will try to run it in a script and see if that helps. I may will comeback to you later.
# 3  
Old 03-01-2013
There was a problem when the file my_inputfile has more than one line. There was a break after the first line. I had to add -n after ssh.
Now, it's OK

I want to modify the beginning of the script like that.
But it doesn't work. Have you an idea ?

Code:
#!/bin/bash
# set -vx
f1()    {
          server=$(hostname -s)
          if [ "$server" == "$6" ]; then
                 cde=""         
           else
                cde=$(ssh $6)
           $cde echo $1 $2 $3 $4 $5 $6
        fi
        }

# 4  
Old 03-02-2013
Try to eval that line, but be aware that the usage of eval is highly discouraged due to security reasons.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to Append the output of a script running in remote server to a file in local server?

Hi guys, So i am in server1 and i have to login to server 2, 3,4 and run some script there(logging script) and output its result. What i am doing is running the script in server2 and outputting it to a file in server 2 and then Scp'ing the file to server1. Similarly i am doing this for other... (5 Replies)
Discussion started by: srkmish
5 Replies

2. Solaris

Script to get files from remote server to local server through sftp without prompting for password

Hi, I am trying to automate the process of fetching files from remote server to local server through sftp. I have the username and password for the remote solaris server. But I need to give password manually everytime i run the script. Can anyone help me in automating the script such that it... (3 Replies)
Discussion started by: ssk250
3 Replies

3. Shell Programming and Scripting

Execute a local script against a remote server

I am unable to run the below script against a remote server due to syntax error (then unexpected), but i am able to run it locally. Am i executing it correctly or is there any other way to execute it. ssh username@servernname ksh -s < scriptname #!/bin/ksh function record { ((end =... (5 Replies)
Discussion started by: NarayanaPrakash
5 Replies

4. Shell Programming and Scripting

How to create a file in remote server from local server?

hi, can anyone tell me how can i store the names of a file that are in remote server to a file and then get the file that contains the name in local server? there is a remote server where 5 files are present , say a.txt b.txt c.txt d.txt e.txt i want to write a script in my local... (4 Replies)
Discussion started by: Little
4 Replies

5. Shell Programming and Scripting

Executing local script/command on remote server

I have a command that I want to run on machine B from machine A. If I run the command on machine B locally, it works fine. Here is the command: for n in `find /data1/ -name 'ini*.ext'` ; do echo cp $n "`dirname $n `/` basename $n .ext`"; done From machine A, I issue this command ... (3 Replies)
Discussion started by: dirtyd0ggy
3 Replies

6. Shell Programming and Scripting

preserving the timestamp of a file when copied from remote server to local server using ftp

Hi, I need to copy few files from remote server to local server. I write a shell script to connect to the remote server using ftp and go to that path. Now i need to copy those files in the remote directory to my local server with the timestamp of all those files shouldnt be changed. ... (5 Replies)
Discussion started by: arunkumarmc
5 Replies

7. Shell Programming and Scripting

Can a script runned in local server access remote server?

Hi, Im creating a script that is supposed to run commands on remote server using sftp. My script is as below: #!/bin/ksh sftp remote_server mypassword cd /u08/mydir/allfiles mget * .. But this is what I got when I runned the script: Connecting to remote server...... (3 Replies)
Discussion started by: luna_soleil
3 Replies

8. Shell Programming and Scripting

Transfer file from local unix server to remote server

want to remove this thread. thanks (2 Replies)
Discussion started by: indira
2 Replies

9. HP-UX

Transfer file from local unix server to remote server

want to remove the thread thanks (2 Replies)
Discussion started by: indira
2 Replies

10. Shell Programming and Scripting

FTP multiple files from remote server to local server

Hi, I am facing a weired problem in my FTP script. I want to transfer multiple files from remote server to local server everyday, using mget * in my script. I also, want to send an email for successful or failed FTP. My script works for file transfer, but it don't send any mail. There is... (2 Replies)
Discussion started by: berlin_germany
2 Replies
Login or Register to Ask a Question