0

Hello I was wondering how it would be possible to create a shell script from an existing shell script? I am currently having already a script done which should be created, it looks like this:

#!/bin/bash

uptime=$(</proc/uptime)
uptime=${uptime%%.*}
user=$(whoami)
seconds=$(( uptime%60 ))
minutes=$(( uptime/60%60 ))
hours=$(( uptime/60/60%24 ))
days=$(( uptime/60/60/24 ))
echo -e "
\033[0;35m+++++++++++++++++++: \033[0;37mServer Data\033[0;35m :+++++++++++++++++++
\033[0;35m+  \033[0;37mHostname \033[0;35m= \033[1;32m`hostname`
\033[0;35m+    \033[0;37mKernel \033[0;35m= \033[1;32m`uname -r`
\033[0;35m+      \033[0;37mTime \033[0;35m= \033[1;32m`date +%k:%M:%S`
\033[0;35m+    \033[0;37mUptime \033[0;35m= \033[1;32m$days d ~ $hours h ~ $minutes m ~ $seconds s
\033[0;35m+       \033[0;37mCPU \033[0;35m= \033[1;32m`cat /proc/cpuinfo | grep 'model name' | cut -c 14-100 | uniq`
\033[0;35m+    \033[0;37mMemory \033[0;35m= \033[1;32m`cat /proc/meminfo | grep MemTotal | awk {'print $2'}` kB
\033[0;35m++++++++++++++++++++: \033[0;37mUser Data\033[0;35m :++++++++++++++++++++
\033[0;37m+  \033[0;37mUsername \033[0;35m= \033[1;32m$user
\033[0;35m+  \033[0;37mSessions \033[0;35m= \033[1;32m`who | grep $user | wc -l` of 14
\033[0;35m+++++++++++++++++++++++++++++++++++++++++++++++++++++\e[m 
"

The only problem I see, is that commands like 'cat' or 'grep' would get them echo'ed out interpreted, instead of being just printed out. So that's why I am asking here how something like this could be one.

Thanks in advance

1 Answer 1

2

Try doing this using here-doc and the special trick with the single quotes around (no interpolation) :

cat<<'EOF'>script.sh
#!/bin/bash

uptime=$(</proc/uptime)
uptime=${uptime%%.*}
user=$(whoami)
seconds=$(( uptime%60 ))
minutes=$(( uptime/60%60 ))
hours=$(( uptime/60/60%24 ))
days=$(( uptime/60/60/24 ))
echo -e "
\033[0;35m+++++++++++++++++++: \033[0;37mServer Data\033[0;35m :+++++++++++++++++++
\033[0;35m+   \033[0;37mAddress \033[0;35m= \033[1;32m109.163.233.49
\033[0;35m+  \033[0;37mHostname \033[0;35m= \033[1;32m`hostname`
\033[0;35m+    \033[0;37mKernel \033[0;35m= \033[1;32m`uname -r`
\033[0;35m+      \033[0;37mTime \033[0;35m= \033[1;32m`date +%k:%M:%S`
\033[0;35m+    \033[0;37mUptime \033[0;35m= \033[1;32m$days d ~ $hours h ~ $minutes m ~ $seconds s
\033[0;35m+       \033[0;37mCPU \033[0;35m= \033[1;32m`cat /proc/cpuinfo | grep 'model name' | cut -c 14-100 | uniq`
\033[0;35m+    \033[0;37mMemory \033[0;35m= \033[1;32m`cat /proc/meminfo | grep MemTotal | awk {'print $2'}` kB
\033[0;35m++++++++++++++++++++: \033[0;37mUser Data\033[0;35m :++++++++++++++++++++
\033[0;37m+  \033[0;37mUsername \033[0;35m= \033[1;32m$user
\033[0;35m+  \033[0;37mSessions \033[0;35m= \033[1;32m`who | grep $user | wc -l` of 14
\033[0;35m+     \033[0;37mAdmin \033[0;35m= \033[1;32mSergey Ruslan
\033[0;35m+++++++++++++++++++++++++++++++++++++++++++++++++++++\e[m 
"
EOF
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.