As the error says applist cannot be executed, remove the ticks..,
Once you have solved the read issue, you find yourself with $app being the complet line, how will solve that?
I would use awk...
Since I don't know your knowledge of Unix, I will show it can be done just with unix commands, but this is dirty:
And now my command line: but I use new line for clarity:
MacBook-Pro wks % cat nn|tr ":" " "|while read X1 X2
do
echo " "$X2" runs on "$X1
done
hrms runs on manhlx138p
sales runs on manhlx142p
finance runs on manhlx143p
IT runs on manhlx145p
It would be great that seeing what you did wrong, come back with a nice script:
We try to avoid the use of cat in such cases as it has an impact on performance
Try to find a solution using awk, if you are going for a loop, then we would like to see no cat command in it
I think you wanted for app in `cat applist`
but the for splits on whitespace (space, tab, newline).
Even for app in $(tr ':' ' ' < applist)
is not easy to handle.
Much better is the suggested awk -F: ...
But this is awk (read lines, split to fields separated by a colon)
You can do it in the shell using read in a while loop. It was suggested already, but using tr is not good; an improvement:
while IFS=":" read server app
do
echo "$app runs on $server"
done < applist
The read reads a line from stdin and splits on $IFS that is set just for the read; the fields go to the following variables.
The loop's stdin is redirected from the applist file.
I want to use your WHILE loop to ssh to few servers.
With the below code, I managed to log in (ssh) to remote server. But, line 6 and Line 7 don't seem to work.
Can't I assign $app variable's value to another variable, once I am logged in (ssh) into another Linux machine ?
-- Just one server for now (for testing)
$ cat applist
manhlx138p:hrms
while IFS=":" read server app
do
echo "$app runs on $server"
ssh -n "oracle@$server" " ### This starting double quote should not be in a separate line
hostname;
APP_NAME=$app; ## Line 6
echo $APP_NAME ## Line 7
"
done < applist
Output of above command. Output for echo $APP_NAME (Line 7 of above WHILE loop) is missing