In the example below: what’s the expected behaviour of the XOR operator in shell? So when we enter what triggers anothercommand to run (if it will execute at all)? Answer If you enter command ^ anothercommand, you are simply providing command with two arguments, ^ and anothercommand. There is no operato…
Tag: bash
Comparing two numbers but if condition does not work correctly
i am new to bash script. I am making a script to calculate cpu usage. So what I am doing is calculating cpu usage and comparing with a predefined value i.e threshold value. But my if condition is always going wrong. Please help me to correct my script. Please do check the above script in bash beacause I have …
Weird recursive directory structure
Somehow, my system (Ubuntu WSL layer on Windows; so treat as exactly Ubuntu) went a bit crazy. For a directory (a git repo if it matters) /path/to/foo, running ls shows the git files ORIG_HEAD, index, and index.lock in there. I tried to debug this by going cd .git, but an ls there showed me my same list of fi…
Linux bash script symlink with spaces
I am trying to make a script which will find all the folders and files within a given hard drive. then i want to make the symlinks to another folder. However the ln command fails if the folder or file contains space, can someone help me to modify the ln -sf $MOVIE_FILE_LINKS -t $MOVIES_LIB command to make the…
linux files and folders are not inheriting parent directory permissions
I created a directory /share and gave chmod 2770 permission and chown root:stock /share. 1) When I create touch a file inside /share, I see the file has rw-rw-r– and I don’t see rwxrws— 2) When I create a directory in /share/data I see the permission as drwxrwsr-x where are the parent direct…
Print grep Keyword if grep find a match
I have an input file Input.txt with the following sample keywords: I also have a file Text.txt to search such as: I want to print grep Keyword followed by a match if the grep finds a match. The desired output: Answer With awk you could do something like this: In a more readable format it would be:
Using sed piped with w command show user with the largest idle time
the w command produces something like this: I must get a sed script that prints the user with the longest idle time. But the problem is that w shows the idle time in 3 different formats: 1) in seconds: ending with an ‘s’, 2) mm:ss (not ending with ‘m’ or ‘s’), and 3) hh:mm …
Adding value to an associative array named after a variable
I need your help with a bash >= 4 script I’m writing. I am retrieving some files from remote hosts to back them up. I have a for loop that iterate through the hosts and for each one tests connection and the start a function that retrieves the various files. My problem is that I need to know what gone
Using variables as the input to command
I’ve scoured various message boards to understand why I can’t use a variable as input to a command in certain scenarios. Is it a STDIN issue/limitation? Why does using echo and here strings fix the problem? For example, As you can see I get the error… ‘File name too long’ Now, I …
Processing stdout and stderr separately adding a timestamp: Wrong order
These are my test files: std-test.sh: process.sh: std-test.sh creates twenty lines containing their line number, and process.sh is able to read another commands output when piped to it, while it saves to stderr when err is passed to it as argument, and to stdout when out is passed. The command I use to test t…