LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-20-2015, 02:49 AM   #1
redhatwannabe
Member
 
Registered: Dec 2008
Posts: 83

Rep: Reputation: 15
Is there A Way To Echo Or Display What Is Being Executed In The Command Terminal


Dear all,

currently in the command prompt, I need to execute a long list of commands

Code:
grid@node1:~/rac/troubleshoot_scan_name> ping -c 3 cluster-scan

grid@node1:~/rac/troubleshoot_scan_name> ping -c 3 node1-vip

grid@node1:~/rac/troubleshoot_scan_name> ping -c 3 node2-vip
..........
this is what I want to achieve,

write a bash script to execute the a list of command below, which can be done. However how do I include, echo or display the prompt, grid@node1:~/rac/troubleshoot_scan_name> naturally?

currently inside my bash script
Code:
export CURRENT_DIRECTORY="$(/bin/pwd)"
export REPLACEMENT="~"
export PROMPT_END="${CURRENT_DIRECTORY/${HOME}/${REPLACEMENT}}"
export PROMPT="${CURRENT_USER}@${HOSTNAME}:${PROMPT_END}> "

printf "\n${PROMPT}ping -c 3 cluster-scan\n"
ping -c 3 cluster-scan
While the above code achieve the above, it is quite error prone. What if whatever display does not reflect what actually happen?


for example

Code:
printf "\n${PROMPT}ping -c 3 cluster-scan\n"
ping -c 3 node1-vip
many thanks in advance!
 
Old 10-20-2015, 03:09 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
What's the point in echoing the prompt again and again?
What do you really want to achieve?

You want a script that executes these commands and generates a log that proves you have executed these commands manually?

Edit: If you only want to see the commands the script executes, use the following:
Code:
set -x
for i in node1-vip node2-vip; do
    ping -c 3 "$i"
done

Last edited by NevemTeve; 10-20-2015 at 03:12 AM.
 
Old 10-20-2015, 03:24 AM   #3
redhatwannabe
Member
 
Registered: Dec 2008
Posts: 83

Original Poster
Rep: Reputation: 15
You want a script that executes these commands and generates a log that proves you have executed these commands manually?

Yes I want a script that executes these commands and generates a log that proves you have executed these commands manually?

That will also means including the prompt "grid@node1~/rac/troubleshoot_scan_name> "

Many thanks in advance!
 
Old 10-20-2015, 03:45 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Code:
#!/bin/sh
echo "This output wasn't generated by a script I promise"

cat <<DONE |
ls -ld .
id -a
echo other commands you executed manually
DONE
while read line; do
    printf '%s@%s:%s$ %s\n' "$USER" "$(hostname)" "$(pwd)" "$line"
    eval "$line"
done

Last edited by NevemTeve; 10-20-2015 at 06:11 AM.
 
1 members found this post helpful.
Old 10-21-2015, 01:22 AM   #5
redhatwannabe
Member
 
Registered: Dec 2008
Posts: 83

Original Poster
Rep: Reputation: 15
Hi NevemTeve, thanks for your solution:

My final solution is as follow:

Code:
export HOSTNAME="$(/bin/hostname)"
export CURRENT_DIRECTORY="$(/bin/pwd)"
export REPLACEMENT="~"
export PROMPT_END="${CURRENT_DIRECTORY/${HOME}/${REPLACEMENT}}"
export PROMPT="${CURRENT_USER}@${HOSTNAME}:${PROMPT_END}> "

cat <<DONE |
ls -lrt
--list of command
DONE
while read line; do
        printf '%s%s\n' "${PROMPT}${line}"
        eval "${line}"
done
again my solution is not really perfect as from http://linuxconfig.org/bash-prompt-basics, the linux prompt can be changed, once changed, the code also need be modified accordingly


thanks a lot!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] timestamp of command history doesn't show time command was executed? UprLftCstr Linux - Newbie 3 04-17-2014 12:31 PM
ShellScript executed from command prmpt but not executed from crontab or at command BMMadhav Linux - Newbie 1 11-16-2012 07:20 PM
[SOLVED] Command to display current terminal window's settings? mrm5102 Linux - Newbie 5 09-07-2011 11:26 AM
How to Display Commands to be Executed with At lrt Linux - Software 1 04-11-2008 11:26 AM
which command executed on which terminal dsids Linux - Newbie 1 09-23-2006 08:57 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration