[SOLVED] Simple Shell Scripting Question - Echo and Execute?

I've got a bash script which has many different commands it executes in a row. Each time the script executes one of the commands, it outputs what it is doing to the screen:
echo "cp -r home new"
cp -r home new
echo "mv testfile new/."
mv testfile new/.
I am just wondering if there is a function to execute a command and output it at the same time. Something like...
echoandexecute("cp -r home new")
echoandexecute("mv testfile new/.")
Last edited by tony5429 (2008-08-27 19:59:04)

Otherwise you can define it yourself:
echoandexecute() {
echo $1
$1
echoandexecute "cp -r home new"
Last edited by catwell (2008-08-27 20:57:16)

Similar Messages

  • [SOLVED] advanced shell script for mount and unmount samba share

    Good morning guys,
    Today I've a very long question....
    Hope in your help....:
    I usually move from different network.
    For one of those I need to mount some share that are under an ActiveDirectory server.
    To do that I use samba.
    Especially I've made and use the below  shell script.
    I know that it's so stupid but I'm a very newbie:
    #!/bin/sh
    echo "This is a custom script for mount my citrix share"
    echo "Please insert user password (must a sudoer user)"
    echo " "
    sudo mount -t cifs -o username='myusername',password='mypassword' //host_ip/TsHome$/myusername /home/myuser/samba_share/TsHOME
    sudo mount -t cifs -o username='myusername',password='mypassword' "//host_ip/direction" /home/myuser/samba_share/direction
    echo " "
    read -p "Press ENTER key to close this terminal"
    exit
    As I say the script work but it's so crude...!
    I want/need to implement it whit the follow addictions:
    1. print a feedback on mount: somenthing like "TsHOME is now mounted" or "unable to mount TsHOME"
    2. check if the share are already mounted: to prevent accidentally multiple mount. If a share is already mounted I want to print something like "TsHOME is already mounted on /home/myuser/samba_share/TsHOME"
    3. ask for share password  (now it's in clear on script) - This is optinal
    4. un-mount the share before reboot or shutdown command. That because I've notice that If i reboot or shutdown without manually un-mount the share the step of unmounting network file system became very slow
    If it's no extremely difficult, can someone help me to write this script?
    I know that man exist but I'm not a programmer and it's so difficult for me approch it.
    Thank you in advance.
    Ale
    Last edited by Alexbit (2010-09-23 21:29:13)

    Thank you very (VERY) much!
    I've follow your information and.. it work!
    If can be usefull to other this is the complete script:
    #!/bin/sh
    echo "This is a custom script for mount citrix share"
    echo "****************************************"
    echo " "
    echo "Please insert domain password:"
    read -s mypassword
    echo " "
    echo "|checking mount state...in share are not mounted mount it!|"
    echo "+-------------------------------------------------------------------------+"
    if grep "TsHome" /etc/mtab &>/dev/null; then
    grep "TsHome" /etc/mtab | awk '{print "TsHome is already mounted on " $2}'
    else
    sudo mount -t cifs -o username='yourusername',password="$mypassword" //ipaddress/TsHome$ /home/Samba_share/TsHOME && echo "TsHOME is now mounted" || echo "unable to mount TsHOME"
    fi
    echo " "
    if grep "direction" /etc/mtab &>/dev/null; then
    grep "direction" /etc/mtab | awk '{print "direction is already mounted on " $2}'
    else
    sudo mount -t cifs -o username='yourusername',password="$mypassword" '//ipaddress/direction' /home/Samba_share/direction' && echo "direction pat is now mounted" || echo "unable to mount direction"
    fi
    echo " "
    if grep "Area" /etc/mtab &>/dev/null; then
    grep "Area" /etc/mtab | awk '{print "Area is already mounted on " $2}'
    else
    sudo mount -t cifs -o username='yourusername',password="$mypassword" '//ipaddress/Area' /home/Samba_share/Area && echo "Area is now mounted" || echo "unable to mount Area"
    fi
    echo " "
    echo "All DONE!"
    echo " "
    read -p "Press ENTER key to close this terminal"
    exit
    I don't really well understand what I've to put in rc.local.shutdown....
    I think somethings like:
    #!/bin/bash
    # /etc/rc.local.shutdown: Local shutdown script.
    echo "This is a custom script for UN-mount citrix share"
    echo "********************************************"
    echo " "
    echo "|check mount state: if mount then un-mount!|"
    echo "+-------------------------------------------------------+"
    echo " "
    if grep "TsHome" /etc/mtab &>/dev/null; then
    grep "TsHome" /etc/mtab | awk '{print "TsHome is NOT mounted -> going on..." $2}'
    else
    sudo umount /home/Samba_share/TsHOME && echo "TsHOME is now UN mounted" || echo "unable to UN-mount TsHOME"
    fi
    echo " "
    if grep "direction" /etc/mtab &>/dev/null; then
    grep "direction" /etc/mtab | awk '{print "direction is NOT mounted -> going on.." $2}'
    else
    sudo umount /home/Samba_share/direction && echo "direction is now UN mounted" || echo "unable to UN-mount direction pat"
    fi
    echo " "
    if grep "Area" /etc/mtab &>/dev/null; then
    grep "Area" /etc/mtab | awk '{print "Area is NOT mounted - Finished" $2}'
    else
    sudo umount /home/Samba_share/Area && echo "Area is now UN mounted" || echo "unable to UN-mount Area"
    fi
    echo " "
    echo "going shutdown"
    echo " "
    sleep 5
    I've try but it seems to ignore the IF statement... this is the output when ALL share are UNmounted:
    $ sh /etc/rc.local.shutdown
    This is a custom script for UN-mount citrix share
    |check mount state: if mount then un-mount!|
    +-------------------------------------------------------+
    umount: /home/Samba_share/TsHOME: not mounted
    unable to UN-mount TsHOME
    umount: /home/Samba_share/direction: not mounted
    unable to UN-mount direction
    umount: /home/Samba_share/Area: not mounted
    unable to UN-mount Area
    going shutdown
    Tomorrow I will test again when I'm at office.
    Meanwhile can you check if I made a macroscopic mistakes?
    Thank you again!
    Last edited by Alexbit (2010-09-23 17:34:49)

  • Simple Shell Script Question.... [java related]

    Hey guys! This is my first post, as im new here :S
    I have a simple problem for a big program.
    We have a .sh to install it, but when I run the .sh in terminal like i should, It says the class is not found. I believe it has to do with the syntax, as the person who made it is not a linux pro. I don't know MUCH about shell scripts, but I'm pretty sure I know where the error lies.
    Our Script:
    java -classpath ./:./jars/tools.jar:./jars/nexus.jar impsoft.nexus.installer.Install
    chmod a+x run.sh compile.sh
    The Error:
    Exception in thread "main" java.lang.NoClassDefFoundError: impsoft/nexus/installer/Install
    What I think the problem is:
    ./jars/nexus.jar impsoft.nexus.installer.Install
    Thank you for ALL of your help!

    Hi Justin,
    Have you tried running the lines from the command line?
    The second thing you may want to try is changing the relative directory for the jar file from a relative one to a fixed directory. From the error, it appears that the install program is running but it is not able to locate a library mentioned in the program (which is probably the jar file listed in the classpath).
    Hope that helps.
    J. Haynes
    Denver

  • Creating simple shell script packages to deploy with ARD and TaskServer

    I am looking for a simple step by step on how to create a package that can be deployed using ARD, to run a simple shell script like
    "softwareupdate -i -a"
    A brief search here returned nothing, but perhaps I was not using the correct terms.
    Ultimately, I want to use ARD to run software update on ~400 Macs.
    Thanks in advance for your help.
    Bill

    If I send it as a unix command, it will run only on machines that are currently awake and responding to ARD.
    If I can set it up as a package, then I can use Task Server to "deploy" the command to machines that are not currently online. When the machines next contact the Task Server, they will be told to run softwareupdate.

  • Running a simple shell script

    Not being a regular user of terminal on OSX, I have managed to forget how to run a simple shell script. The purpose of the script is to rename a set of files contained in a specific directory. Here is a sample of the script I wrote some time back:
    mv product_22.jpg 080688614423.jpg
    This command repeats for each file I need to rename. My recollection is that I simply put the actual script text file in the same directory as the images to be renamed and drag the script file into a new terminal window and hit enter to run. When I try this however I receive the following error:
    ord2: Permission denied
    Can anyone help me out here? I'm running 10.2.8. I seem to beforgetting a critical step somewhere along the way.
    Thanks
      Mac OS X (10.2.x)  

    Have you set the execution bit for the script? When not, use: chmod 755 script.

  • Shell Script  for Startup and Shutdown the database

    Hi,
    i want Shell Script for Startup and Shutdown the database in Solaries.
    could any one can hep me where i can get this script. or send to me to [email protected]
    Thanks & Regards,
    Gangi reddy

    SHUTDOWN
    SHUTDOWN ABORT]
    Shuts down a currently running Oracle instance, optionally closing and dismounting a database.
    Terms
    Refer to the following list for a description of each term or clause:
    ABORT
    Proceeds with the fastest possible shutdown of the database without waiting for calls to complete or users to disconnect.
    Uncommitted transactions are not rolled back. Client SQL statements currently being processed are terminated. All users currently connected to the database are implicitly disconnected and the next database startup will require instance recovery.
    You must use this option if a background process terminates abnormally.
    IMMEDIATE
    Does not wait for current calls to complete or users to disconnect from the database.
    Further connects are prohibited. The database is closed and dismounted. The instance is shutdown and no instance recovery is required on the next database startup.
    NORMAL
    NORMAL is the default option which waits for users to disconnect from the database.
    Further connects are prohibited. The database is closed and dismounted. The instance is shutdown and no instance recovery is required on the next database startup.
    TRANSACTIONAL [LOCAL]
    Performs a planned shutdown of an instance while allowing active transactions to complete first. It prevents clients from losing work without requiring all users to log off.
    No client can start a new transaction on this instance. Attempting to start a new transaction results in disconnection. After completion of all transactions, any client still connected to the instance is disconnected. Now the instance shuts down just as it would if a SHUTDOWN IMMEDIATE statement was submitted. The next startup of the database will not require any instance recovery procedures.
    The LOCAL mode specifies a transactional shutdown on the local instance only, so that it only waits on local transactions to complete, not all transactions. This is useful, for example, for scheduled outage maintenance.
    Usage
    SHUTDOWN with no arguments is equivalent to SHUTDOWN NORMAL.
    You must be connected to a database as SYSOPER, or SYSDBA. You cannot connect via a multi-threaded server. For more information about connecting to a database, see the CONNECT command earlier in this chapter.
    http://download-west.oracle.com/docs/cd/B10501_01/server.920/a90842/ch13.htm#1013607
    Joel Pérez

  • Simple Shell Script To Add To Default Path?

    I know that this question will at first appear idiotic,but here goes.
    I'm logged into a Solaris 9 shell as 'root'. I echo $PATH and see a couple of default paths. I want to add to that PATH - this can easily be done from the command line as follows:
    PATH=$PATH:/some/other/path
    export PATH
    So, naturally, I'd like to throw this into a simple script, and when I do so, and run the script called 'hello'. So, I run 'hello' as follows:
    ksh hello (I'm a kshell)
    Result: no errors. Then, I check my PATH as follows:
    echo $PATH
    And nothing changes, it's still the same PATH!
    Can anyone tell me how to do this?
    Appreciate your response.
    dedham_ma_man

    The question shows you don't really understand how shells work.
    The path is part of the environment of your current shell.
    Any command you "execute" is run in a new process. And any changes made to the environment of that process cannot effect the environment of the parent process (ie your shell).
    So by saying "ksh hello" your starting a new shell and changing the path in that shell. But when that shells exits and your back in the original shell, of course nothing has changed.
    So the answer is that what you have to do is not "run" the script. But persuade your current shell to execute in its own context.
    In a C shell that would be done by the "source" command. And in a bourne shell, its the "." command. But I'm not sure how you do it in korn shell.

  • Shell script for moving and renaming files

    I'm not so familiar with shell scripts. I have created some simple scripts for various tasks but now I hit something I can't handle myself.
    My server receives every night a logfile from another server. This file I need to rename and move to another location. Problem is that a simple mv with new filename isn't enough, I need to insert date in filename. Format for date should be ddmmyy. Also the name of incoming file changes every night. The output always starts with same letters, let's say "log", and the date is used to identify files.
    For example I receive a file /Input/filename.log and I need to move the file to the folder /Output with name log130308.txt.
    Any tips how to insert the date as part of of filename while renaming or moving the file? I prefer shell script as I'll use launchd for scheduling the script.

    Hi again,
    so far I've constructed this:
    cd /input
    for file in `ls *log`
    do
    echo "Copying " $file
    cp $file /output/log.$file.`date +%d%m%y`
    done
    Quite there. I'm cding to directory, i'll focus later how to set some environmental parameters.
    Problem with zipping is that I don't know exact logfiles names. Each day there's a new name. So zip -q filename.zip filename.log isn't quite enough and it's impossible to use wildcards. The switch -q and checksums were welcome suggestions.
    Thanks again,
    Juha
    Message was edited by: J.Otava

  • How to excute a power shell script to remote machine using power shell script folder path and script name

    Hi,
    Let say, I have 3 parameters.
    1. Script FolderPath (Remote path for e.g \\RD101\ScriptSharedFolder     Here RD101 is one server)
    2. Script Name(StopAllService.ps1)
    3. Server Name (RD45)
    I want to execute a powershell scritp in my local machine(Test1)  and in that script I want to pass the above three parameters.Now I want to excute the StopAllService.ps1 script into RD45 server. But the script is available in RD101 machine.
    So What I want to here How can we do this ? I have script name and script folder path and target execution server name.
    Pls giude me or give me the script.
    By
    A Path Finder..
    JoSwa 
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful"
    Best Online Journal

    Hi,
    You got 2 solutions for your problem:
    1- If you have permission to run scripts in the remote computer without specifying your credential,
    then, the first reply solve your problem.
    2- If you have permission to run scripts in the remote computer
    having to specify your credential, l then,
    my solution solves your problem.
    If the remote computer requires signed scripts only, you need signed script. Period. Or are you trying to break remote computer security?
    If you need signed script, there's no psdrive that'll circumvent such requirement.
    The problem is that the execution policy is set to "RemoteSigned". Using the URL explicitly tags that script as being from a remote source, and the policy blocks it.  The PSDrive provides a local reference for the script.  It does not
    sign the script but the local drive reference may prevent it from being blocked for being from a remote source. I'll test that later.
    Script signing is not and should never be considered a security measure. It is easily circumvented by running the script using powershell.exe, and using the -ExecutionPolicy parameter to override whatever the local execution policy setting is. 
    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

  • Simple shell script utility to copy abs tree for build

    Hi,
        I'm learning shell script and decided to make a simple script to copy the folder i want from /var/abs to
        a new folder so i can build the package. here it is: (remember, i'm new to shell scripting, if you know a better way to do it, you can modify my script
        and post here a better solution, so, i (and others) can learn with my errors)
    #!/bin/bash
    #Looks for the abs tree of the software you want
    #and copy it to your build path
    ABSTREE=/var/abs
    echo -n "What software do you want? "
    read absname
    result=$(find $ABSTREE -name $absname)
    for i in $result; do
    echo -n "$i, is this what you want? [y/n] "
    read opt
    if [ $opt = "y" ]; then
    echo -n "Copy to... "
    read buildpath
    relative=${i#$ABSTREE}
    absolute=$buildpath${relative%$absname}
    mkdir -p $absolute
    cp -r $i $absolute
    echo "$i successfully copied to $absolute"
    exit 0
    fi
    done
    exit 1

    spoonman wrote: mkdir -p $absolute
    cp -r $i $absolute
    echo "$i successfully copied to $absolute"
    You are assuming `mkdir` and `cp` were successful. You should test them to make sure, and exit with failure if not:
    mkdir -p $absolute || exit 1
    cp -r $i $absolute || exit 1
    echo "$i successfully copied to $absolute"
    You could also include your own error message, but mkdir and cp would throw their own if something fails...
    mkdir -p $absolute || { echo "mkdir failed"; exit 1; }
    cp -r $i $absolute || { echo "cp failed"; exit 1; }
    echo "$i successfully copied to $absolute"
    Another way is to use `set -e` which will exit the script on any failure without explicit testing:
    set -e
    mkdir -p $absolute
    cp -r $i $absolute
    set +e
    echo "$i successfully copied to $absolute"

  • Shell scripting question

    I'm writing a shell script and I need to get $PWD. There's a cd command in my script, and my script needs to navigate back to the directory in which it was executed. Is this possible? If so, how would I go about doing it? Sorry if this is in the wrong board.

    In the future, you may want to post Unix and Terminal oriented questions to the Mac OS X Technologies > Unix Forum
    <http://discussions.apple.com/forum.jspa?forumID=735>

  • Simple shell script problem...

    I am trying to get a list of directories on a remote machine, compare them with the directories on the local machine and copy any that dont match on the local machine to a backup folder. Heres my code but i am a newbie and i cant get my shell script to read from a file. The for loop never executes so i am guessing i am not reading the file correctly - see below.
    #!/bin/ksh
    ssh -l removeserver 192.168.xxx.xxx ls /remote/server/directory/ > /store/remote/directory/listing/motapp1files.txt
    newfile=''
    $newfile < /store/remote/directory/listing/motapp1files.txt
    for file in $newfile;
    do echo $file
    if [ ! -d local/machine/directories/$file/ ]; then
    echo "this folder doesnt exist on on remote server "; echo $file;
    fi
    done

    Re-inventing the wheel perhaps. Look into rsync. Great program!
    I used it in FreeBSD to do a very similar task.

  • Shell Script behaves different and wrong on OEL5 then SUSE 11

    Hi all ,
    I execute following script on SUSE11 and receive the following output as expected and as it should be.
    $ echo "\nxxxx\tyyyy\ndddd"
    &lt;newline&gt;
    xxxx&lt;tab&gt;yyyy
    dddd
    $
    But when I execute same command on OEL 5.2 , following output is coming which shows that "newline" and "tab" are not recognized and behaved as text.
    $echo "\nxxxx\tyyyy\ndddd"
    \nxxxx\tyyyy\ndddd
    Please can some one explain to me why .
    I have many shell script and they are not working properly now on OEL.

    Hi ,
    I had a look to man page of echo and seen that -E option of echo command is enabled by default.
    I craeted an alias echo="echo -e" in user profile and scripts are running now properly.
    Thank you .
    Leo.

  • Shell Script For Export And Import Of Table Records

    Hello,
    We have production and test instances and for constant testing we need to copy data from production to test or development environment.
    At the moment what we do is manually doing export and import table records. At times this could be very tedious as we may need
    to do this exercise a couple of times in a day.
    Is it a good idea to do this exercise using shell script? If so how could I do this? If this is not a good idea what are the best alternatives?
    Any input is highly appreciated.
    Thanks

    Ah I see, your company prefers stupidity over efficiency. It would be possible to do it in a controlled environment, wouldn't it? Also the test database would be allowed to select only.
    So the non-allowance is just plain stupid.
    To the second question: do you use hard-coded passwords in shell scripts?
    Don't you think that poses a security risk?
    Don't you think that is a bigger risk than a database link, properly set up?
    In my book it is!
    Sybrand Bakker
    Senior Oracle DBA

  • Simple shell script issue

    Hi,
    I have an issue with following shell script:
    #!/bin/sh
    env | while read VAR; do
    RIGA=$RIGA"\""$VAR"\";"
    done
    echo $VAR
    echo $RIGA
    exit 0
    Why the last echo commands (echo $VAR and echo $RIGA) don't give me any result? Why $VAR and $RIGA are empty?

    From what I understand, anything to the right of a pipe is run in a sub-process in non POSIX shells, which runs in it's own environment. Variables changed inside ta sub-shell are not changed at the parent process.
    Perhaps using ksh instead of bourne shell will work, or you could try input redirection rather than using pipe command. e.g.:
    while read VAR; do
    RIGA=$RIGA"\""$VAR"\";"
    done < $(env)
    echo $VAR
    echo $RIGA
    exit 0
    Edited by: Dude on Dec 15, 2010 6:11 AM

Maybe you are looking for

  • Screen resolution preferences does not work for me...

    I have a 1920x1080, 17.4 laptop. That is approx 126.6 ppi screen resolution if my calculkations are correct... So in order to set PS to display real size of the images according to the zoom, I go to edit / preferences / units & rulers and set the scr

  • Order of starting services in Hyperion System 11.1.1 release

    Hi All, How important is the order of starting services in 11.1.1 ? I mean does this needs to be done manually every time or does Hyperion takes care of this automatically (including dependencies), if I set this to "automatic" in the services panel?

  • What is the webconference tool of choice for demos from a linux box?

    Since conference.oracle.com and strtc only work on IE, what are folks doing when they want to do a webconference from a linux box? Are people installing IE (I see it is possible but sounds a bit hacky), or is there another oracle conference tool or a

  • Can't connect ad-hoc network after IOS 8.2 update

    Hi, This is my first post on Apple Discussion so please ignore grammatically and other mistakes if i made. I am using iPhone 5S 32 GB factory unlocked and working very well in INDIAN network.  I use to connect ad-hoc network created on my Window 7 De

  • Using JavaFX on secure (SSL) pages

    On IE, when displaying a JavaFX applet on a secure page, a dialog is displayed warning the user: "This page contains both secure and nonsecure items. Do you want to display the nonsecure items?" At least some of the remote resources required for Java