how do I call multiple commands in a single variable for bash script? [closed] - linux

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have set of commands and I want all of them to be defined in a single variable. below are the commands I want display in output.
pwdx `ps -ef | grep java | cut -d' ' -f4` | cut -d/ -f7
I tried with
app=`pwdx `ps -ef | grep java | cut -d' ' -f4` | cut -d/ -f7`
echo $app
but this gives empty output. Tried using for loop, that failed too.

Use the |& operator to pipe the output (stdout and stderr) of the previous command into the standard input of another one:
pwdx `ps -ef |& grep java |& cut -d' ' -f4` |& cut -d/ -f7
See this write-up for more on executing several bash commands: Running multiple commands in one line in shell

Related

Bash Console putting some invisible chars into string var [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Below I shared my console. I want to cut some string from output of some commands.
But there are 17 extra chars which I have no idea where comes from.
Can someone pls explain to me?
$ ls -al | grep total | sed 's/[[:blank:]].*$//' | wc -m
23
$ ns="total"
$ echo $ns | sed 's/[[:blank:]].*$//' | wc -c
6
But there are 17 extra chars which I have no idea where comes from.
Those are ANSI escape codes that grep uses for coloring matching substrings. You probably have an alias (run alias | grep grep to examine) like
alias grep='grep --color=always'
somewhere that causes grep to color matches even if output is not a tty, or something similar.
Try
ls -al | grep --color=never total | sed 's/[[:blank:]].*$//' | wc -m
and you'll get six.

IFCONFIG value is not passed into another variable in shell [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Here is my piece of code:
CONFIG= ifconfig lo;
MAC_ADDRESS="$("${CONFIG}" | awk '/HWaddr/ {print $NF}' | sed -e 's/://g')";
MAC_ADDRESS2="$("${CONFIG}" | awk '/HWaddr/ {print $NF}' | sed -e 's/://g')";
echo "$MAC_ADDRESS"
echo "$MAC_ADDRESS2"
Here I am trying to set ifconfig lo into a single variable and try passing into another one where it is needed.
So my expectation is, it should pass like below
MAC_ADDRESS="$(ifconfig lo | awk '/HWaddr/ {print $NF}' | sed -e 's/://g')";
MAC_ADDRESS2="$(ifconfig lo | awk '/HWaddr/ {print $NF}' | sed -e 's/://g')";
Here I get permission denied exception. Any other possible way to do?
Expected Output:
JUKUJSDJDJSJDJSJ
KIIPPSKKSKDKDKKS
note: this answer assumes bash. Other shells have different behaviour.
If you attempt something similar like this:
bash-4.2$ command="ls"
bash-4.2$ $command
bash-4.2$ "$command"
Then you will notice that you execute ls in both cases. However, if you attempt the following:
bash-4.2$ command="ls -l"
bash-4.2$ $command
will work as expected (this fails in zsh), but if you quote it like
bash-4.2$ "$command"
bash: ls -l: command not found
it fails.
You attempt a similar thing but you have quoted the command in your command-substitution $(command).
In short: do not
CONFIG= ifconfig lo;
MAC_ADDRESS="$("${CONFIG}" | awk '/HWaddr/ {print $NF}' | sed -e 's/://g')";
but do
CONFIG="ifconfig lo"
MAC_ADDRESS="$(${CONFIG} | awk '/HWaddr/ {print $NF}' | sed -e 's/://g')";
I also fixed your definition of CONFIG as it needs <double-quotes> and spaces are not allowed before and after the <equal>-sign (=)

Sorting numbers in a row on the BASH / Shell [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
There is a line:
00000000000000;000022233333;2;NONE;true;100,100,5,1,28;UNKNOWN
It is necessary to sort 100,100,5,1,28 numbers in descending order.
Example:
00000000000000;000022233333;2;NONE;true;100,100,28,5,1;UNKNOWN
try this;
#!/bin/bash
while read line
do
beforeC=$(echo "$line" | cut -f-5 -d';')
sortcolumn=$(echo "$line" | awk -F ";" '{print $6}' | tr -t , "\n" | sort -r -n | xargs | sed 's/ /,/g')
afterC=$(echo "$line" | cut -f7- -d';')
echo -e $beforeC";"$sortcolumn";"$afterC
done <file
user#host:/tmp/test$ cat file
00000000000000;000022233333;2;NONE;true;100,100,5,1,28;UNKNOWN
00000000000000;000022233333;2;NONE;true;99,100,5,1,28;UNKNOWN
00000000000000;000022233333;2;NONE;true;100,99,5,1,28;UNKNOWN
00000000000000;000022233333;2;NONE;true;100,100,4,1,28;UNKNOWN
00000000000000;000022233333;2;NONE;true;100,100,4,0,28;UNKNOWN
00000000000000;000022233333;2;NONE;true;100,100,4,1,27;UNKNOWN
user#host:/tmp/test$ ./sortAColumn.sh
00000000000000;000022233333;2;NONE;true;100,100,28,5,1;UNKNOWN
00000000000000;000022233333;2;NONE;true;100,99,28,5,1;UNKNOWN
00000000000000;000022233333;2;NONE;true;100,99,28,5,1;UNKNOWN
00000000000000;000022233333;2;NONE;true;100,100,28,4,1;UNKNOWN
00000000000000;000022233333;2;NONE;true;100,100,28,4,0;UNKNOWN
00000000000000;000022233333;2;NONE;true;100,100,27,4,1;UNKNOWN

Scripting with unix to get the processes run by users [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
If I find out I have two users logged (UserA and UserB) in to the systems right now, How do i find out the processes run by those two users. but, the trick here is the script is to be run in an unattended batch without any input from the keyboard. other than being invoked.
I know the first part of the script would be
who | awk '{print $1}'
the output of this would be
UserA
UserB
What I would like to know is, how can I use this output and shove it with some ps command automatically and get the required result.
I finally figured out the one-liner I was searching for, with the help of the other answers (updated for case where no users logged in - see comments).
ps -fU "`who | cut -d' ' -f1 | uniq | xargs echo`" 2> /dev/null
The thing inside the backticks is executed and "inserted at the spot". It works as follows:
who : you know what that does
cut -d' ' : split strings into fields, using ' ' as separator
-f1 : and return only field 1
uniq : return only unique entries
xargs echo : take each of the values piped in, and send them through echo: this strips the \n
2> /dev/null : if there are any error messages (sent to 2: stderr)
: redirect those to /dev/null - i.e. "dump them, never to be seen again"
The output of all that is
user1 user2 user3
...however many there are. And you then call ps with the -fU flags, requesting all processes for these users with full format (you can of course change these flags to get the formatting you want, just keep the -U in there just before the thing in "` `"
ps -fU user1 user2 user3
Get a list of users (using who), save to a file, then list all processes, and grep that (using the file you just created),
tempfile=/tmp/wholist.$$
who | cut -f1 -d' '|sort -u > $tempfile
ps -ef |grep -f $tempfile
rm $tempfile
LOGGED_IN=$( who | awk '{print $1}' | sort -u | xargs echo )
[ "$LOGGED_IN" ] && ps -fU "$LOGGED_IN"
The standard switch -U will restrict output to only those processes whose real user ID corresponds to any given as its argument. (E.g., ps -f -U "UserA UserB".)
Not sure if I'm understanding your question correctly, but you can pipe the output of ps through grep to get the processes run by a particular user, like so:
ps -ef | grep '^xxxxx '
where xxxxx is the user.

Is something wrong with my use of wc or grep in the linux command line? I"m getting +1 on my char count [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
When I run
echo "obase=2;3" | bc | grep -v \n\s | wc -m
bash returns 3. But when I run
echo "obase=2;3" | bc
bash returns 11.
Why is wc -m one digit high on its count?
The extra character is the trailing newline.
wc -m receives and counts the following three characters: 1 1 \n.
$ echo "obase=2;3" | bc | grep -v \n\s | od -c
0000000 1 1 \n
0000003
If you get rid of the newline, the count will be as you're expecting:
$ echo "obase=2;3" | bc | grep -v \n\s | tr -d '\n' | wc -m
2

Resources