Read user input (which should be a linux command) and execute - linux

I wish to write a simple csh script which loops through all computers in a network and executes a command that is input at the command line
echo -n "Please enter command you would like executed on all computers > \n "
set command = "$<"
say the user enters ls | grep something. How would I then execute this command in the following line? I tried
$command which works fine for input such as echo "Hello World". I get the following error for ls | grep something
ls: |: No such file or directory
ls: grep: No such file or directory
ls: something: No such file or directory
Ideally, I would want to enter several commands at the command line before looping through each computer in the network (which I can already do) and execute. Eg say I wish to copy two different files
sudo cp ./bin/elastix /usr/bin; sudo cp ./lib/transformix /usr/lib
Thanks

loops through all computers in a network and executes a command that is input at the command line
You would be perhaps using ssh so you would do something like:
ssh $hostname "$command"
I'm not a csh user so I may have the syntax wrong. For current machine you may use the eval shell command that should interpret any command sequences, not only simple commands.

Related

how to execute ssh comand on .sh file?

I trying to create a .sh file that execute things like "pwd" or "ls" command.
My problem its when i execute the .sh file.
Its seems not recognize the tasks
I tried to use echo
Example : echo 'lsApps' or echo "lsApps"
but it prints the name of the task instead execute the comand
for example i want to execute a .ssh file that makes a pwd
VAR_1=pwd
echo $VAR_1
but it prints me pwd instead the current path ...
Any idea?
echo is used to print on the screen (man page reference). If you do echo 'IsApps' it will take it as a string and print it. If you want to execute a command you can just do it by doing IsApps (acutes not quotes, acute is usually below the escape key). This will execute the command and show the output on the screen. If you want to store the output of the command in a variable, you can do
<variable_name>=`IsApps`
This will store the output in the variable. Note that there is no space between variable name and the command. Also, those are not quotes but instead acutes. To print the variable on screen you can use echo by doing echo $<variable_name>
If you don't want to see the output at all. You can do
IsApps > /dev/null
this will execute the command but you will not see any stdout on your screen.
As far as ssh is concerned, do ssh-keygen and then ssh-copy-id user#remote_ip to set ssh keys so that you don't have to enter your password with ssh. Once you have done that, you can use ssh user#remote_ip in your shell script.

Unix Script Looping cat file while read line "No such file or directory" error

I have a script the reads a parameter file, and is going to do some actions with the values of each line in that script. My input file has spaces as separators.
The weird thing is, it works on an old version of Linux but not on a newer version.
#! /bin/ksh
su root "cat /var/opt/OV/tmp/HPOV_gg.log" | while read Line
do
echo "${Line}"
done
Error: bash: cat /var/opt/OV/tmp/HPOV_gg.log: No such file or
directory
The error has obv something to do with the new Linux version, parsing the cat command in a different way.
How can I fix this? Or can I rewrite my script to let it work on this new Linux version.
It's better to use sudo to execute commands as root. No quotes are needed, and sudo access can be controlled in a fine grained manner via its configuration file.
sudo cat /var/opt/OV/tmp/HPOV_gg.log | while ...
Just so you know, you could fix your su command by writing su root -c "cat file". Commands need to be passed via the -c option. But still, sudo is better.

Putty executing a text tile in a linux terminal

I have a one liner that I run from the run box in the start menu:
"C:\Program Files (x86)\casper\PuTTY\putty.exe" -ssh "192.168.1.2" -l casper -pw "<password>" -m \\PROD.MSAD.casp.NET\UserData\CASPER\Home\Documents\pbauth_list.txt -t
It sssh to a linux box, and opens up a terminal and echos these statements intoa terminal. when someone needs elevated access at the linux terminal i just type in user=user than cut and paste the line into the command line - and viola that user has access to a super user account for what ever time period i designate. It works awesome.
The
\\PROD.MSAD.casp.NET\UserData\CASPER\Home\Documents\pbauth_list.txt text
file looks like this
echo "pbrun pbauthcl grant PBAUTH_P_A4_PROD \$user 2 \"\$user needs access\" now all
pbrun pbauthcl grant PBAUTH_P_A4_UTIL \$user 2 \"\$user needs access\" now all
printf '\e[8;50;100t'
"
Notice that the
"printf '\e[8;50;100t'"
is echoed out along with the lines into the terminal .
What this command does is automagically logs into a linux box at 192.168.1.2 and then opens up a terminal and prints the lines to the terminal.
i cut and paste the print statement "printf '\e[8;50;100t'" to resize the terminal.
When a user wants elevated access at the linux command prompt
What I do is type in user= and then cut and paste one of the pbun command and it gets executed at the linux command line - which works fine.
however it would be much cooler if the "printf '\e[8;50;100t'" would just execute and make the terminal larger instead of me having to echo it into the terminal and then cut and paste it into the same terminal to resize it.
I have tried a bunch of different permutations in the test file to get the printf statement to just run, instead of echoing it out.
bash -v echo "printf '\e[8;50;100t'"
bash ;
I get thse kinds of errors though
/usr/bin/printf: /usr/bin/printf: cannot execute binary file
/bin/echo: /bin/echo: cannot execute binary file
how do I execute the "printf '\e[8;50;100t'"
command within the terminal instead of echoing it out ? There has to be a way .
Two possibile solutions that might work for you are the following:
Try -x
Try -o verbose

Shell Scripting 101

Here is my assignment for class: I know you don't post specific questions, but here's what I have tried and it isn't working. I was hoping for someone to point me in the direction and I can go from there:
Write a shell script that performs the following functions:
as the first command in your shell script use the script -a ch10-q1.txt command.
use the echo command to display the hostname, logname, and home system variables.
displays the current date and time using Coordinated Universal Time.
displays the list of directories in the user's home directory and all the subdirectories below (hint: use the tree command with the appropriate option).
list all the files in the user's home directory and all the subdirectories below (hint: check the options). Also use the -gF options.
use the df command to display the space usage in your system. Use the option(s) to include the total size in human readable format.
Save your shell script file in your home directory and name it ch10-1
Here is my code in vi ch10-1.
# !/bin/bash
script -a ch10-q1.txt
echo $hostname
echo $date -u
echo $ls -d */u
echo $ls -la
echo $ls gf
echo $df; df -h
Then I save the file and make the file executable by:
chmod 777 ch10-1
I try and run the program by:
./ch10-1
And then it tells me that line 2-8 command not found.
I guess my questions is how do I have multiple commands?
$hostname
The command to display the hostname.

read user input without interruption in bash

I'm trying to grep the user command into a variable.
If the command begins with specific words I'm redirecting the command into another shell (not BASH), if not it will run regularly on bash.
When I'm using "read" I lose the BASH prompt then I'm unable to auto-complete, backspace etc.
My goal is:
if I type for example ls -la the command will not run and will be assigned into variable:
user#machine:~$ ls -la
user_command = 'ls -la'
if the command is not other shell command (not BASH)
eval $user_command
Is there any way to achieve this?
I guess you want to : invoke some specific commands through another shell instead of bash
You could define a function for each of those commands
exemple:
ls () { # function that, instead of bash, launch 'ls' through ksh
ksh -c "ls $*"
}
that way, with each function's name matching the command you want to run with another shell (edit the inside of the function accordingly if it needs to be run with another shell than ksh)
Place those function in specific user's "~/.bashrc" files.
You could write your implementation of ls and place it in ~/bin/ls.
Give full permissions to this file

Resources