I trying to append passed text as parameter to file using shell script
this is the code of shell
echo $1>>/etc/freeradius/mods-enabled/ldap
this shell will get the text to add it to the file ldap in /etc/freeradius/mods-enabled path , I call this shell in this format
# sh /etc/append.sh hello how are you man
but in this example the shell only get first word 'hello' and append it to file .how can I tell shell that all words are same variable and should insert to file
It depends on the shell you're using.
In bash the this script should work:
#!/bin/bash
echo "${#:1}">>/etc/freeradius/mods-enabled/ldap
Edit: As DTSCode pointed out in comments the :1 part in the script is redundant so this would be more correct:
#!/bin/bash
echo "$#" >> /etc/freeradius/mods-enabled/ldap
Then give the permissions to execute the file and call
/etc/append.sh hello how are you man
Or without execute permission call
bash /etc/append.sh hello how are you man
Related
suppose we have "token=1234" content present in a filename. how to get the complete value 1234 and store it in variable called token in shell script
So that I can use $token to use it
If you're using Bash, eval is your friend.
eval "token=1234"
echo $token # should give you 1234
Also, can you explain what content present in a filename means? Are you trying to parse the file name or the file's content?
If it's the latter you can just execute your script like any other script:
./yourfile.sh
If the file contains just the line token=1234 then:
source filename
echo "$token"
I created a .sh file that is supposed to run the cat command on a text file then replace part of the text and echo it back.
The file name that I'm running the cat command on is "New" with the content Hello and the executable script is:
alias message="cat ~/ll/New"
echo "${message//ello/X}
alias command does not assign a variable
So ${message) is empty
Probably you should use message as a command not as variable
Example one
#!/bin/sh
# purpose: print out current directory name and contents
pwd
ls
Example two
# purpose: print out current directory name and contents
#!/bin/sh
pwd
ls
What is the difference – if I make the first line a comment(#), with #!/bin/sh as the second line, what will happen?
What is meaning of #!/bin/sh ?
Normally a shell script is run by your default shell defined in the /etc/passwd file. But you can define explicitly a program which can run your script.
Unices uses a common method to determine what program needed to run a specific script (man execve(2)). If the script has the proper execute rights set and in a script the first line starts with a #! characters, it will run by the program defined afterwards.
For example if the first line is #!/usr/bin/awk -f then the rest of the file will be passed to the awk program (so it has to use awk syntax). Or if a Makefile starts with #!/usr/bin/make -f then the rest of the file will be passed to make. You can start the script as a normal program and the script can be written in awk or make (or whatever defined) syntax.
If execve does not find #! as the first two character of the file, it will consider as a normal script file and it will run as it is.
So using #! You can determine the script language and You do not need to know what shell is used by the other user using your script. In any other line #! will be interpretered your default shell, which is usually just a comment line.
what is difference between 1st & 2nd shell scripts..?
No difference in output. But the time to execute both will be little different as the interpreter reads line one by one.
if i give comment(#) in 1st line after #!/bin/sh in 2nd line so what will happen ?
Any line started with (#) except the shebang(#!) is treated as a comment in shell script.
what is meaning of #!/bin/sh ?
Its the path(here - /bin/sh) to the interpreter used after the shebang (#!) . Shell will try to use the interpreter language mentioned after the shebang to execute the script.
I'm trying to write a shell script that builds an iso image from an .asm file with several utilities. When you run the script from the command line it first asks the user where the file is and where the iso file should be. I want the script accept arguments the user passed as part of the command: ./mkiso foo.iso bar.asm? The two arguments would correspond to two variables, input and output. How do I do this? EDIT: I'm using Linux and my script is a bash script.
mkiso:
#!/bin/bash
input=$1
output=$2
echo "input: $input, output: $output"
Run it with parameters
./mkiso aa.iso bb.asm
Output
input: aa.iso, output: bb.asm
This is my first time trying to create a terminal script and then using AppleScript to run the code. I've tested my terminal script line by line in the terminal (it works...), but would like to put it in a file that can be called by applescript. So far, I've created a file called "/Applications/MAMP/htdocs/global_admin/import_database_command_line.sh" where I've saved all of the individual commands, the first being:
/Applications/MAMP/Library/bin/mysql --host=localhost -uroot -proot;
I then use AppleScript to call the script as:
do shell script
"/Applications/MAMP/htdocs/global_admin/import_database_command_line.sh"
Unfortunately, for each line in my script I get an error, such as:
error "/Applications/MAMP/htdocs/global_admin/import_database_command_line.sh:
line 1: : command not found
Any help in coordinating my AppleScript and the file that contains my shell commands would be appreciated.
You need to include #!/bin/sh in the top line of your .sh file
#!/bin/sh
echo "Hello, world!"
You then need to make the script executable:
chmod +x import_database_command_line.sh
This is because the 'do shell script' AppleScript command is expecting to run the shell commands contained within the quotes. It is not expecting to be calling another script as you are doing. Just put your commands within the quotes, separated by semi-colons.
do shell script "/Applications/MAMP/Library/bin/mysql --host=localhost -uroot -proot; #other_command; #etc"
Link