Linux Bash Script that can take Unlimitd Arguments [duplicate] - linux

This question already has answers here:
How do I use a variable argument number in a bash script?
(3 answers)
Closed 6 years ago.
What's going on everyone? I have this assignment in my Linux Operating System class and im having a little trouble. It asks me to,
Write a Bash shell script called chkread that takes an unlimited number of
arguments that all represent file names.
I have come up with this so far, but i don't think it's exactly what the professor is looking for.
~$ cat MY_SCRIPT
#!/bin/bash
echo ${unlimited arguments}
~$ bash MY_SCRIPT cat dog horse
cat dog horse

You could use something like this to handle multiple arguments in a bash script.
#!/bin/bash
file_names=("$#")
for name in "${file_names[#]}"; do
echo "$name"
done
And then when you call the script:
bash chkread.sh file1 file2 file3 file4
The script will print them to output:
file1
file2
file3
file4
But this is just an example. Inside the script you can do with them whatever you need to do.

Use $*. That will give you everything. For example:
#!/bin/bash
file_names=$*
echo $file_names
Output:
jbanks#efsappdev1:~$ x.sh `ls *.sql`
current.sql goop.sql latest.sql long.sql report.sql
jbanks#efsappdev1:~$ x.sh one two three
one two three

Related

How to pre-specify a selection when executing a program on Linux [duplicate]

I have a bash script that employs the read command to read arguments to commands interactively, for example yes/no options. Is there a way to call this script in a non-interactive script passing default option values as arguments?
It's not just one option that I have to pass to the interactive script.
Many ways
pipe your input
echo "yes
no
maybe" | your_program
redirect from a file
your_program < answers.txt
use a here document (this can be very readable)
your_program << ANSWERS
yes
no
maybe
ANSWERS
use a here string
your_program <<< $'yes\nno\nmaybe\n'
For more complex tasks there is expect ( http://en.wikipedia.org/wiki/Expect ).
It basically simulates a user, you can code a script how to react to specific program outputs and related stuff.
This also works in cases like ssh that prohibits piping passwords to it.
You can put the data in a file and re-direct it like this:
$ cat file.sh
#!/bin/bash
read x
read y
echo $x
echo $y
Data for the script:
$ cat data.txt
2
3
Executing the script:
$ file.sh < data.txt
2
3
Just want to add one more way. Found it elsewhere, and is quite simple.
Say I want to pass yes for all the prompts at command line for a command "execute_command", Then I would simply pipe yes to it.
yes | execute_command
This will use yes as the answer to all yes/no prompts.
You can also use printf to pipe the input to your script.
var=val
printf "yes\nno\nmaybe\n$var\n" | ./your_script.sh

How to format linux mpstat output in multiple lines [duplicate]

This question already has answers here:
Capturing multiple line output into a Bash variable
(7 answers)
Closed 3 years ago.
I have a small script where I appended the output of linux mpstat to a log file.
#/bin/bash
CPU_USAGE=$(mpstat)
echo $CPU_USAGE >> temp.log
The problem is that the output of mpstat on the terminal is formatted properly in 3 lines like so
However, the output to the file is all in one line.
How do I format the output like the one on the terminal?
Just quote the variable so it is not seen as several different parameters to be printed one after the other:
echo "$CPU_USAGE" >> temp.log
You could just directly pipe the output to the file:
#!/bin/bash
mpstat >> temp.log
If you must store it in a variable, then quote it like:
#!/bin/bash
CPU_USAGE=$(mpstat)
echo "$CPU_USAGE" >> temp.log
Otherwise, bash will not interpret the newlines as part of the message to echo, but the whole output as a list of short strings to output.

Bash - echo username with backslash to file [duplicate]

This question already has answers here:
Why do backslashes disappear when run through echo?
(3 answers)
Closed 4 years ago.
Linux newbie here, but I have a script that is supposed to create a file:
VI_USERNAME=domain\\user
echo "VI_USERNAME=$VI_USERNAME" >> .visdkrc
File looks like this:
VI_USERNAME=domain user
How can I get my output to looks like this:
VI_USERNAME=domain\user
This does not happen with bash with the code you describe (protip: always test your own example):
$ cat myfile
VI_USERNAME=domain\\user
echo "VI_USERNAME=$VI_USERNAME" >> .visdkrc
$ bash myfile
$ cat .visdkrc
VI_USERNAME=domain\user
However, something similar happens with dash when the username starts with a "t":
$ cat myfile
VI_USERNAME=domain\\thatotherguy
echo "VI_USERNAME=$VI_USERNAME" >> .visdkrc
$ dash myfile
$ cat .visdkrc
VI_USERNAME=domain hatotherguy
(you may be using sh, which calls dash and not bash on Debian based distros since 2011)
The general rule about echo is that if you have to ask, use printf instead:
VI_USERNAME=domain\\user
printf '%s\n' "$VI_USERNAME" >> .visdkrc
echo is a legacy command that treats data differently across platforms and shells, especially with regard to flags and backslash sequences.
printf is well defined and has fewer pitfalls.

With a bash script that utilizes system commands; how would you have it input when the COMMAND asks you for something? [duplicate]

This question already has answers here:
Passing arguments to an interactive program non-interactively
(5 answers)
Closed 7 years ago.
So suppose a normal command run in terminal goes like this....
user$ thecommand
Please enter your first name:
>
and then waits for your to type your name... straightforward, but if in a bash script I try and do something like:
#! /bin/bash
echo "What is your name?"
read name
thecommand
how would I have THE SCRIPT enter "$name" in response to "thecommand" instead of having the user manually input it themselves?
you can add input by pipe like this:
echo yourname | ./yourscript
for more inputs you can use printf
printf "input1\ninput2" | ./yourscript
where \n means new line and it will be used like new input.
Run your script like:
./yourscript.sh < file.txt
where file.txt will contain the name.
now your script will look for name from the file(file.txt), in file.txt you can type the names which will act as input for read command.
read command reads on line at a time so if u have more than on read command in your script you should have multiple lines in file.txt file
For complicated cases, for example if your input depends on the output of your command, you may write an "expect" script.
To see how it works you can auto-generate such script interactively
$ autoexpect thecommand
And then run it
$ expect -f script.exp

basic Linux bash command clarifications: awk and others [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I am using Mac OS and Bash terminal to do some basic scripting.
I have some questions which I could not find answers when I searched online
(or may be my search keywords were bad)
Firstly, I see there are three paths: /bin, /usr/bin and /usr/local/bin. some commands like grep are found in /usr/bin, while some others like ls are found in /bin.
why we have three different bins and what criteria goes to put commands like these over there
Secondly, I want to know the difference between using ' and `.
echo `date`
Fri Jan 10 10:36:52 PST 2014
awk '{print $1}' test.txt
1
2
3
4
if I try:
awk `{print $1}` test.txt
-bash: {print: command not found
awk: syntax error at source line 1
context is
>>> test. <<< txt
awk: bailing out at source line 1
so when to use ' and `.
Lastly, the above awk with print works. but this does not work
awk '{echo $1}' test.txt
can print and echo not be used interchangebly?
P.S: I am a beginner with bash scripting, please be kind
Brief answers not necessarily exhaustive:
/bin is usually for system commands
/usr/bin is for commands for users
/usr/local/bin is for software not typically installed by a distribution or release of an OS.
Quotes:
Things in single quotes are not touched by the shell
Things in double quotes are variable expanded by the shell ($var is expanded to what $var contains)
Things in back quotes are executed as a command and the output of that command replaces what was in the back quotes. You can also use $(echo Hello) to achieve the same thing.
You cannot easily mix awk and shell inside a string passed as a script to awk. (ex: awk '{echo $1}' test.txt)
awk `{print $1}` test.txt
This fail since you are using back tics and not single quotes.
Correct:
awk '{print $1}' test.txt
It will then print first field of all line in the file.
awk '{echo $1}' test.txt
Does not work since echo is not an awk command.
Tell us what text you have and what you like to get out of it..
You need to read a book. I recommend Shell Scripting Recipes: A Problem-Solution Approach (http://www.amazon.com/Shell-Scripting-Recipes-Problem-Solution-Approach/dp/1590594711). Read it, work through the exercises, and then come back with questions if you have any (or even better ask them at the comp.unix.shell newsgroup where that book's author and all the other shell experts hang out).
print is an awk command.
echo is a shell command.
backticks and $() run shell commands.
' quotes the contents such that the shell does not expand anything inside them.
$(echo hello) is the same as: echo hello. By doing this you ask the bash interpreter to execute the result of that command which is "hello". But hello is not a command (unless if you define it yourself).
These examples are all correct:
foo=$(echo hello)
echo ${foo}
foo=(echo "echo hello")
${foo}

Resources